uniapp项目的使用方式

  • 安装依赖

    1. $ npm install weex-bindingx --save
    2. import BindingX from weex-bindingx;

    1. const Binding = uni.requireNativePlugin('bindingx');
    2. const domModule = weex.requireModule('dom');
  • 编写对应的表达式

    • 据业务场景,选择您需要的EventType。 比如,要监听手势,evenType值为pan,监听滚动容器scrollOffset变化,eventType值为scroll。
    • 根据交互行为,选择要改变的属性,并编写相应的表达式。比如,交互行为是”用户横滑100单位,透明度从1变化到0”。则属性为”opacity”,表达式为”1-x/100”。
  • 绑定表达式

    1. var result = BindingX.bind({
    2. eventType: 'pan', ==> 事件类型
    3. anchor: 'foo', ==> anchor指的是事件的触发者,如果是eventType"orientation""timing",则不用填
    4. props: [
    5. {
    6. element: view.ref, ==> 要改变的视图的引用或者id
    7. expression: "1-x/100", ==> 表达式
    8. property: "opacity" ==> 要改变的属性
    9. }
    10. ]
    11. })
  • 解绑

    1. BindingX.unbind({
    2. token: result.token,
    3. eventType: 'pan'
    4. })

属性详解

  1. eventType:事件类型(必选)
  2. pan:监听手势
  3. timing:监听时间变化,用来做动画
  4. scroll:监听容器scroll运动
  5. orientation:监听设备方向运动
  6. exitExpression:边界条件(可选)
  7. props:运行时参数(必选)
  8. element:作用元素,获取元素见下方getEl方法
  9. property:作用的属性
  10. expression:运行时的表达式
  • property详解 | 属性 | 功能 | | —- | —- | | transform.translate | x&y方向平移 | | transform.translateX | x方向平移 | | transform.translateY | y方向平移 | | transform.scale | x&y方向缩放 | | transform.scaleX | x方向缩放 | | transform.scaleY | y方向缩放 | | transform.rotateZ | 绕z轴旋转(2d) | | transform.rotateX | 绕x轴旋转(3d) | | transform.rotateY | 绕y轴旋转(3d) | | opacity | 透明度 | | width | 宽度 | | height | 高度 | | background-color | 背景色 | | color | 文字颜色 | | scroll.contentOffset | 控制scroller#contentOffset | | scroll.contentOffsetX | 控制scroller#contentOffsetX | | scroll.contentOffsetY | 控制scroller#contentOffsetY |

简单的动画运动

  1. <view class="container">
  2. <text ref="orderFlush" class="container-text">一去二三里</text>
  3. </view>
  1. const Binding = uni.requireNativePlugin('bindingx');
  2. const domModule = weex.requireModule('dom');
  3. export default {
  4. data() {
  5. return {
  6. };
  7. },
  8. onLoad: function() {
  9. this.init();
  10. },
  11. methods: {
  12. getEl: function(el) {
  13. if (typeof el === 'string' || typeof el === 'number') return el;
  14. if (WXEnvironment) {
  15. return el.ref;
  16. } else {
  17. return el instanceof HTMLElement ? el : el.$el;
  18. }
  19. },
  20. init () {
  21. this.$nextTick(() => {
  22. let result = domModule.getComponentRect(this.$refs.orderFlush, option => {
  23. let order = this.getEl(this.$refs.orderFlush);
  24. let width = uni.upx2px(750);
  25. this.orderBinding = Binding.bind({
  26. eventType: 'timing',
  27. exitExpression: 't>6000',
  28. props: [{
  29. element: order,
  30. property: 'transform.translateX',
  31. expression: 'linear(t, 0,'+(-width*2-option.size.width)+',8000)'
  32. }]
  33. }, (res) => {
  34. if (res.state === 'exit') {
  35. //解绑操作
  36. Binding.unbind({
  37. token: this.orderBinding,
  38. eventType: 'timing',
  39. });
  40. this.orderBinding = '';
  41. uni.showToast({
  42. title: '运动结束',
  43. icon: 'none'
  44. })
  45. }
  46. });
  47. });
  48. });
  49. }
  50. }
  51. }
  1. .container {
  2. width: 750rpx;
  3. height: 100%;
  4. flex-direction: column;
  5. }
  6. .container-text {
  7. position: absolute;
  8. left: 500rpx;
  9. top: 20rpx;
  10. background-color: rgba(0,0,0,.5);
  11. padding: 10rpx 30rpx;
  12. color: #FFFFFF;
  13. font-size: 20rpx;
  14. border-top-left-radius: 10rpx;
  15. border-top-right-radius: 10rpx;
  16. border-bottom-left-radius: 10rpx;
  17. border-bottom-right-radius: 10rpx;
  18. transition-property: opacity;
  19. transition-duration: 1.3s;
  20. transition-delay: 0s;
  21. transition-timing-function: ease-out;
  22. }

消息滚动通知

消息通知很多场景都会用到,下面说下在uniapp项目中的应用。

#c3动画实现

如果是H5页面,或者不用太考虑性能,可以使用css3动画去实现。
思路:
1.文字默认在手机屏幕右侧向左边滚动,首先定位元素到750rpx处,此时不可见。
2.计算元素的宽度,滚动距离应为(元素宽度+屏幕宽度),滚动速度可自行定义,下面我写的是10s.
2.使用keyframes实现匀速无限滚动.

  1. animation: name 10s linear infinite;
  2. @keyframes name {}

#bindingX实现

如果你很了解bindingX的使用,那将会很简单的实现,大致思路和上方一致,难点是运动公式的计算。大致代码如下

  1. getEl (el) {
  2. if (typeof el === 'string' || typeof el === 'number') return el;
  3. if (WXEnvironment) {
  4. return el.ref;
  5. } else {
  6. return el instanceof HTMLElement ? el : el.$el;
  7. }
  8. },
  9. init () {
  10. this.$nextTick(() => {
  11. let result = domModule.getComponentRect(this.$refs.orderFlush, option => {
  12. let order = this.getEl(this.$refs.orderFlush);
  13. let page_width = uni.upx2px(750);
  14. setTimeout(()=>{
  15. domModule.getComponentRect(this.$refs.orderFlush, option => {
  16. const width = option.size.width;
  17. const time = Number(width) * 1000 / 40;
  18. this.orderBinding = Binding.bind({
  19. eventType: 'timing',
  20. exitExpression: 't>' + time,
  21. props: [{
  22. element: order,
  23. property: 'transform.translateX',
  24. expression: `linear(t, 0, -${page_width + width}, ${time})`
  25. }],
  26. }, (res) => {
  27. if (res.state === 'exit') {
  28. Binding.unbind({
  29. token: this.orderBinding,
  30. eventType: 'timing',
  31. });
  32. /* ---运动结束后重置状态--- */
  33. this.orderBinding = '';
  34. this.left = 750;
  35. /* ---运动结束后轮询--- */
  36. this.init();
  37. }
  38. });
  39. });
  40. },100)
  41. });
  42. });
  43. }

背景色渐变

  1. domModule.getComponentRect(this.$refs.dialogPage, option => {
  2. this.orderBinding = Binding.bind({
  3. eventType: 'timing',
  4. exitExpression: 't>300',
  5. props: [{
  6. element: this.getEl(this.$refs.dialogPage),
  7. property: 'opacity',
  8. expression: {
  9. origin:'t/300'
  10. }
  11. }],
  12. }, (res) => {
  13. if (res.state === 'exit') {
  14. Binding.unbind({
  15. token: this.orderBinding,
  16. eventType: 'timing',
  17. });
  18. console.log('---动画结束---');
  19. }
  20. });
  21. });

缩放

  1. domModule.getComponentRect(this.$refs.dialogloop, option => {
  2. this.orderBinding = Binding.bind({
  3. eventType: 'timing',
  4. exitExpression: 't>300',
  5. props: [{
  6. element: this.getEl(this.$refs.dialogloop),
  7. property: 'transform.scale',
  8. expression: 't< 200 ? 0.2 : t/300'
  9. }],
  10. }, (res) => {
  11. if (res.state === 'exit') {
  12. Binding.unbind({
  13. token: this.orderBinding,
  14. eventType: 'timing',
  15. });
  16. console.log('---动画结束---');
  17. }
  18. });
  19. });

旋转

  1. domModule.getComponentRect(this.$refs.dialogloop, option => {
  2. this.orderBinding = Binding.bind({
  3. eventType: 'timing',
  4. exitExpression: 't>1000000',
  5. props: [{
  6. element: this.getEl(this.$refs.dialogloop),
  7. property: 'transform.rotateZ',
  8. expression: 't / 600 * 50'
  9. }],
  10. }, (res) => {
  11. if (res.state === 'exit') {
  12. Binding.unbind({
  13. token: this.orderBinding,
  14. eventType: 'timing',
  15. });
  16. console.log('---动画结束---');
  17. }
  18. });
  19. });

官方案例

http://dotwe.org/vue/e50f76a6c13337b6fa4201a045c5dc0c
http://dotwe.org/vue/2dff486956044ea59b3d38a2cf20b506
http://dotwe.org/vue/64998432f2a249f5cb35b4de0040526d
http://dotwe.org/vue/cd942c4bee9c4b7bcceda4e3aaf94c70