pallax.wxml 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <view class="floatcontainer" arr="{{arr}}" change:arr="{{go.initArr}}">
  2. <block wx:for="{{arr}}" wx:for-item="nitem" wx:for-index="ndx" wx:key="ndx">
  3. <view class="floatgroup group{{ndx}}">
  4. <t-image src="{{oitem}}" mode="widthFix" t-class="nitempic nitempic{{odx}}" wx:for="{{nitem}}" wx:for-item="oitem" wx:for-index="odx" wx:key="odx" class="nitempicwrap" />
  5. </view>
  6. </block>
  7. </view>
  8. <wxs module="go">
  9. var hasStart = false
  10. var dir = -1;
  11. var speed = 280;
  12. var initArr = function (newVal, oldVal, ownerInstance) {
  13. if (newVal.length > 0 && !hasStart) {
  14. hasStart = true;
  15. var startTime = getDate();
  16. var items = ownerInstance.selectAllComponents(".floatgroup");
  17. var ctn = ownerInstance.getBoundingClientRect().width;
  18. dir = -1
  19. items.map(function (item) {
  20. item.left = 0;
  21. })
  22. function step(timestamp) {
  23. var nowTime = getDate();
  24. if (nowTime - startTime > 20) {
  25. startTime = nowTime;
  26. for (var i = 0; i < items.length; i++) {
  27. var item = items[i];
  28. var movable = item.getBoundingClientRect().width - ctn;
  29. var trans = 0;
  30. if (dir > 0) {
  31. item.left = Math.max(0, item.left - movable / speed)
  32. trans = Math.round(-item.left)
  33. if (item.left <= 0) {
  34. dir = -1
  35. item.left = 0
  36. }
  37. } else {
  38. item.left = Math.min(movable, item.left + movable / speed)
  39. trans = Math.round(-item.left)
  40. if (item.left >= movable) {
  41. dir = 1
  42. item.left = movable
  43. }
  44. }
  45. var transform = 'translate3d(' + trans + 'px, 0, 0)'
  46. var transition = 'transform .6s cubic-bezier(0.18, 0.89, 0.32, 1)'
  47. item.setStyle({
  48. '-webkit-transform': transform,
  49. '-webkit-transition': transition,
  50. 'transform': transform,
  51. 'transition': transition
  52. })
  53. }
  54. }
  55. ownerInstance.requestAnimationFrame(step);
  56. };
  57. ownerInstance.requestAnimationFrame(step);
  58. }
  59. }
  60. module.exports = {
  61. initArr: initArr
  62. };
  63. </wxs>