码迷,mamicode.com
首页 > 移动开发 > 详细

Ant Design -- 图片可拖拽效果,图片跟随鼠标移动

时间:2019-12-18 12:54:39      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:page   efs   default   ||   bsp   bst   def   alt   width   

Ant Design 图片可拖拽效果,图片跟随鼠标移动,需计算鼠标在图片中与图片左上角的X轴的距离和鼠标在图片中与图片左上角的Y轴的距离。

constructor(props) {
        super(props);
        this.dragDrop = false; // 图片是否被拖动中
        this.apartX = 0; // 鼠标在图片中与图片左上角的X轴的距离
        this.apartY = 0; //鼠标在图片中与图片左上角的Y轴的距离
        this.state = {
            value: 1,
            previewVisible: false,
            previewImage: ‘‘
        }
}
    //图片点击查看
    handlePreview = (url) => {
        this.setState({
            previewImage: url,
            previewVisible: true,
        });
    };
    // 关闭图片预览
    closeImgIcon = () => {
        this.setState({
            previewVisible: false
        });
    }
    // 图片跟随鼠标移动
    _mouseDown = (event) => {
        this.dragDrop = true;
        const imgWrap = this.refs.showPreviewImageWrap;
        this.apartX = event.pageX - imgWrap.offsetLeft;// 鼠标在图片中与图片左上角的X轴的距离
        this.apartY = event.pageY - imgWrap.offsetTop;

        event.preventDefault();
        event.stopPropagation();
    }
    _mouseUp = (event) => {
        this.dragDrop = false;

        event.preventDefault();
        event.stopPropagation();
    }
    _mouseMove = (event) => {
        if (!this.dragDrop) {
            return;
        }
        if(this.apartX === 100 || this.apartY === 100){
            return;
        }

        const imgWrap = this.refs.showPreviewImageWrap;
        imgWrap.style.left = (event.pageX - this.apartX)+ px;
        imgWrap.style.top = (event.pageY - this.apartY) + px;


        event.preventDefault();
        event.stopPropagation();
    }
    //图片后缀判断
    imgSuffix = (url) => {
        let suffix = url.substring(url.length - 3);
        return suffix
    };
{this.state.previewVisible &&
    <div className="ant-modal-wrap">
       <div className=show_picture_image_wrap
       onMouseUp={this._mouseUp.bind(this)} 
       onMouseDown={this._mouseDown.bind(this)} 
       onMouseMove={this._mouseMove.bind(this)}
       ref="showPreviewImageWrap" >
          <Icon className="close-imgIcon" type="close-circle" onClick={() => this.closeImgIcon()}/>
          {
            (previewImage.indexOf(.pdf) > 0) ?
               <iframe src={previewImage} frameborder="0"
               style={{width: 100%, height: 800px}}></iframe> :
               <img alt="example"  style={{width: 100%}} src={previewImage}/>
           }
       </div>
    </div>
 }

 

Ant Design -- 图片可拖拽效果,图片跟随鼠标移动

标签:page   efs   default   ||   bsp   bst   def   alt   width   

原文地址:https://www.cnblogs.com/juewuzhe/p/12059041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!