小程序icon滑动带吸附效果

最近有一个需求把icon 实现可以滑动,然后带边缘吸附效果,话不多说,上代码吧。

image.png
xml
<image 
  catchtap="newGift"        
  class="toShop-btn"  
  style="left:{{left}}px;top:{{top}}px;" 
  bindtouchmove="viewTouchMove"
  bindtouchend="viewTouchend"
  wx:if="{{isAuto==true || offflng==0}}" 
  src='https://tmawx.shenzhenair.com/Shwxcharfile/ts/app/newmoney.png'></image>


css
.toShop-btn{
  position: fixed;
  top:481rpx;
  width: 60px;
  height:65px;
  line-height: 48px;
  z-index:9999;
  right:0rpx;
-moz-animation: myfirst 1s infinite; 
-webkit-animation: myfirst 1s infinite;
 -o-animation: myfirst 1s infinite; 
 animation: myfirst 1s infinite;
 background:none;
}

@keyframes myfirst{
0% { transform: scale(.8); }
50% { transform: scale(1); }
100% { transform: scale(.8); }
}





js
   data:{
    left: '',
    top: ''
  }   


viewTouchMove: function (e) {
    const widthHalf = app.globalData.width / 2;
    console.log(widthHalf)
    console.log(e.touches[0].clientX);

    this.setData({
      left: e.touches[0].clientX - 30,
      top: e.touches[0].clientY - 30
    })

    if (e.touches[0].clientX < 0) {
      e.touches[0].clientX = 0;
    };

    //这里要注意一下条件:位移X > 宽度-图片宽度
    if (e.touches[0].clientX > app.globalData.width - 60) {
      e.touches[0].clientX = app.globalData.width - 60;
    };

    if (app.globalData.heighttwo - e.touches[0].clientY - 35 <= 0) {
      e.touches[0].clientY = app.globalData.heighttwo -35;

    };

    if (e.touches[0].clientY < 0) {
      e.touches[0].clientY = 0;
    };
    this.setData({
      left: e.touches[0].clientX-10 ,
      top: e.touches[0].clientY-30
    })
  },
  //拖拽结束后的事件
  viewTouchend(e) {
    
    const widthHalf = app.globalData.width / 2;

    if (e.changedTouches[0].clientX > widthHalf) {
      console.log("我是右边")
      e.changedTouches[0].clientX = app.globalData.width - 60;
    }

    if (e.changedTouches[0].clientX < widthHalf) {
      console.log("我是左边")
      e.changedTouches[0].clientX = 0;
    }
    this.setData({
      left: e.changedTouches[0].clientX - 0,
    
    })
  },

  /* 添加授权判断 */
  //star 修改 点击不要赠送 
  onClose() {
    const that = this;
    console.log('点击关闭我')
    that.setData({
      showUserInfo: false
    })
  },


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.今天在官员群里的交流情绪波动比较大,说出来也好。这个事情的关键点还得继续理顺。 2.今天意识到放弃不是解决问题...
    林中溪水响叮咚阅读 226评论 0 0
  • 秋月的雨儿淅沥沥的下, 伞儿开出五颜六色的花。 我们从四面八方赶来, 迎接我们的是美丽的红马甲。 伞下的她们, 笑...
    AsonMM阅读 437评论 0 1
  • 爱自己,让你的身体“不药而愈” 当人处在爱自己的状态时,处在有爱的状态时,大脑开始释放内啡肽,这是人体自带的天然止...
    风云舞实用育儿365阅读 3,567评论 0 10
  • 尽管没有标志的五官和高挑的身材,但成为一名优雅的女子,是我一直以来的追求。 外在的条件是天生的,优雅气质却得靠修炼...
    邓莹_喜恩健康阅读 1,613评论 0 5