swc-scroll.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // components/page-scroll/page-scroll.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. "className":{
  8. value:"",
  9. type:String
  10. },
  11. "styleSheet":{
  12. value:"",
  13. type:String
  14. },
  15. "upper-threshold":{
  16. value:5,
  17. type:Number
  18. },
  19. "scrollitem":{
  20. value:[],
  21. type:Array
  22. },
  23. "scrollto":{
  24. value:"",
  25. type:Number
  26. }
  27. },
  28. /**
  29. * 组件的初始数据
  30. */
  31. data: {
  32. height:0,
  33. upper:true,
  34. index:0
  35. },
  36. ready:function(){
  37. },
  38. methods: {
  39. scrollLower:function(e){
  40. this.triggerEvent("scrolltolower",e.detail);
  41. },
  42. scrollUpper:function(e){
  43. this.triggerEvent("scrolltoupper",e.detail);
  44. },
  45. scroll:function(e){
  46. var scrolltop = e.detail.scrollTop;
  47. var scrollitem =this.data.scrollitem;
  48. var index = 0;
  49. if(this.data.upper){
  50. if(scrolltop>this.data["upper-threshold"]){
  51. this.setData({
  52. upper:false,
  53. height:0
  54. })
  55. }
  56. }else{
  57. if(scrolltop<this.data["upper-threshold"]){
  58. this.setData({
  59. upper:true
  60. })
  61. }
  62. }
  63. var l = scrollitem.length;
  64. if(scrolltop>scrollitem[l-1]){
  65. index=l;
  66. }else{
  67. for(var i=0;i<l;i++){
  68. if(scrolltop<scrollitem[i]){
  69. index = i;
  70. break;
  71. }
  72. }
  73. }
  74. if(index!=this.data.index){
  75. this.setData({
  76. index:index
  77. });
  78. this.triggerEvent("swc",index);
  79. }
  80. this.triggerEvent("scroll",this.data.upper);
  81. },
  82. changeHeight:function(e){
  83. var _self=this;
  84. if(this.data.starttop){
  85. this.setData({
  86. height:(e.touches[0].pageY-this.data.starttop)/2
  87. })
  88. this.triggerEvent("topheight",this.data.height);
  89. }else{
  90. this.setData({
  91. starttop:e.touches[0].pageY
  92. })
  93. }
  94. },
  95. endChange:function(e){
  96. var obj = {
  97. starttop:0
  98. }
  99. if(this.data.height){
  100. obj.height=0;
  101. }
  102. this.setData(obj);
  103. this.triggerEvent("topheight",this.data.height);
  104. }
  105. }
  106. })