当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序开发图片拖拽实例详解

微信小程序开发图片拖拽实例详解

2018年05月07日  | 移动技术网IT编程  | 我要评论

微信小程序开发图片拖拽实例详解

1.编写页面结构:moveimg.wxml

<view class="container"> 
  <view class="cnt"> 
    <image class="image-style" src="../uploads/foods.jpg" style="left:{{ballleft}}px;width:{{screenwidth}}px" bindtouchmove="ballmoveevent"> 
    </image> 
  </view> 
</view> 

2.编写页面样式:moveimg.wxss

.container { 
  box-sizing:border-box; 
  padding:1rem; 
} 
.cnt{ 
  width:100%; 
  height:15rem; 
  border: 1px solid #ccc; 
  position:relative; 
  overflow: hidden; 
} 
.image-style{  
  position: absolute;  
  top: 0px;  
  left:0px;  
  height:100%;  
}  

3.设置数据:moveimg.js

var app = getapp() 
page({ 
  data: { 
    ballleft:-20, 
    screenwidth: 0, 
  }, 
  onload: function() { 
    var _this = this; 
    wx.getsysteminfo({ 
      success: function(res) { 
        _this.setdata({ 
          screenheight: res.windowheight, 
          screenwidth: res.windowwidth, 
        }); 
      } 
    }); 
 
  }, 
  ballmoveevent: function(e) { 
    var touchs = e.touches[0]; 
    var pagex = touchs.pagex; 
    console.log('宽度 '+this.data.screenwidth) 
    console.log('pagex: ' + pagex); 
    //这里用right和bottom.所以需要将pagex pagey转换  
    var x = this.data.screenwidth/2 - pagex-20; 
    if(this.data.screenwidth>385){ 
      if(x>42){x=42;} 
    }else{ 
      if(x>32){x=32;} 
    } 
    if(x<0){x=0;} 
    console.log('x:' + x) 
    this.setdata({ 
      ballleft: -x 
    }); 
  } 
}) 

   这几天一直在研究图片裁剪,思路是有,可是却遇到各种问题。可怜编程不易啊。

想了好久,决定还是简单开始吧。如果大家有更好的方式或是其他想法,欢迎提出,一起讨论。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网