美文网首页小程序
小程序拖拽效果实现

小程序拖拽效果实现

作者: AstarX | 来源:发表于2018-06-05 20:27 被阅读0次

1. 基础ui组件使用css 帧动画的基础ui

2.创建 wxml 代码如下

ps(此组件可获取用户formid 以便于向用户推送信息)

<!--点赞小心心 -->

<view  class="feed"  bindtouchmove="touchMoveChange"  style="left:{{feed_style.x}};top:{{feed_style.y}};"catchtap='addAnimateFun'>

    <form  bindsubmit="formSubmit"  report-submit="true">

      <button  class="heart {{addAnimate}}"  id="like1"  rel="like"  style="background-position: {{cssAnimate}} center;"  formType="submit"></button>

    </form>
</view>

3.创建wxss ui部分省略 以上可见基础css部分

核心代码

.feed{

position:  fixed;

z-index:  10;

}

button::after{

border:none;

}

4.创建 js文件用于设置组件长宽

(1)主要参数

feed_style:{

x:"",

y:"",

}//这个参数是定位使用的x , y值

仅在js里面传递的参数:

screen:{

        width:"",

          height:""

} // 用于保存屏幕页面信息

preX:上次的x值

preY:上次的y值

(2).核心方法

1.通过系统接入getinfo获取当前界面可用宽高

wx.getSystemInfo({

success: function (res) {

console.log(res);

console.log("platform",res.platform);

console.log(res.model);

// 可使用窗口宽度、高度

console.log('height=' + res.windowHeight);

console.log('width=' + res.windowWidth);

// Math.ceil()

if(res.platform == "android"){

res.windowHeight = res.screenHeight;

}

// feed_style: {

// x: res.windowWidth + "px",

// y: res.windowHeight + "px"

// },

self.setData({

screen:{

width: res.windowWidth ,

height: res.windowHeight ,

pixelRatio: res.pixelRatio,

ratio: res.windowWidth * res.pixelRatio/750

}

})

// 计算主体部分高度,单位为px

// that.setData({

// second_height: res.windowHeight

// })

}

})

2.通过 touchmove 拿到切换值,将切换值赋值给外部接收参数
touchMoveChange(e){

var tmpx = parseInt(e.touches[0].clientX);

var tmpy = parseInt(e.touches[0].clientY);

if (tmpx <= 0 || tmpy <= 0 || tmpx >= this.data.screen.width || tmpy >= this.data.screen.height ){

}else{

if (tmpx != this.data.preX || tmpy != this.data.preY ){

console.log(e.touches[0].clientX, "-X-", e.touches[0].pageX)

console.log(e.touches[0].clientY, "-Y-", e.touches[0].pageY)

this.data.preX = tmpx

this.data.preY = tmpy

this.setData({

feed_style: {

x: tmpx - 50 + "px",

y: tmpy - 50 + "px",

}

})

}

}

}

注意事项:

484EB19B6E1F4852AC33D9C608D62943.png

设置前一位数值,拿到值之后转为整数,防止浮点运算影响效率

直接获取设置 定位值为left:top 降低计算难度

相关文章

  • javascript学习笔记-拖拽效果

    最近开发小程序,用到小程序里的拖拽效果,研究了下底层的原理,发现只要以下三点就可以完成拖拽效果。实现拖拽效果的三个...

  • 小程序拖拽效果实现

    1. 基础ui组件使用css 帧动画的基础ui 2.创建 wxml 代码如下 ps(此组件可获取用户formid ...

  • HTML5拖拽上传

    传统拖拽效果小demohtml5实现拖拽小demo调查问卷小demo拖拽拼图小demo拖拽上传小demo h5拖拽...

  • 拖拽API

    实现拖拽效果 目前实现拖拽效果 HTML5拖拽 源元素事件例子 目标元素事件 从本地拖放图片到页面中 实现拖拽

  • HTML5实现拖拽

    实现拖拽效果源元素 - 要拖拽的文件目标元素 - 要拖拽到哪里去 目前实现拖拽效果使用原生DOM就能实现 - 最麻...

  • HTML5拖拽drag

    通过拖拽实现页面元素的位置改变 实现拖拽效果 源元素 - 要拖拽的文件 目标元素 - 要拖拽到哪里去 目前实现拖拽...

  • 小程序拖拽排序组件 Drag

    仿发朋友圈,图片拖拽排序功能,小程序拖拽排序组件 效果演示 地址:https://github.com/yijin...

  • 原生拖拽,拖放事件(drag and drop)

    拖拽,拖放事件可以通过拖拽实现数据传递,达到良好的交互效果,如:从操作系统拖拽文件实现文件选择,拖拽实现元素布局的...

  • Flutter 仿QQ消息拖拽(贝塞尔曲线实现)

    Flutter 仿qq消息拖拽效果通过自定义view实现,效果如下 如果要实现最终的效果,首先绘制拖拽连接处的线(...

  • 小程序实现拖拽功能

    先看图

网友评论

    本文标题:小程序拖拽效果实现

    本文链接:https://www.haomeiwen.com/subject/kobmsftx.html