小程序拖拽效果实现

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 降低计算难度

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

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,050评论 3 119
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,771评论 19 139
  • 根据统计学习方法写的KdTree实现,### 参考了这个博客的主要思路,但是在关于如何搜索最近邻上有些不同。1.我...
    gibyeng阅读 7,954评论 0 0
  • 人是感情动物,再冷漠的人都可以被打动。 只要你有一颗执着的心,能坚持下来,做事多少是一回事,做事质量和成效是另一回...
    财富健康1阅读 3,502评论 0 2
  • 诗歌、叶孤腾 如果明天会更好 如果努力就会有回报 如果微笑可以带来好运 如果命运掌握在自己手中 、 如果我不愿意去...
    叶孤腾阅读 3,047评论 0 0