html+js模仿知乎滑动效果

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  8. <title></title>
  9. <style>
  10. .big-box{
  11. position: relative;
  12. width: 100%;
  13. display: flex;
  14. justify-content: center;
  15. overflow: hidden;
  16. padding: 10px 0;
  17. height: 130px;
  18. }
  19. .box_1{
  20. width: 90%;
  21. margin: auto;
  22. position: absolute;
  23. box-shadow: -1px 2px 4px #C2C2C2;
  24. border-radius: 5px;
  25. padding: 16px 16px 10px;
  26. background: #fff;
  27. }
  28. .box_1>span{
  29. display: block;
  30. padding: 2px 0;
  31. overflow: hidden;
  32. white-space: nowrap;
  33. text-overflow: ellipsis;
  34. }
  35. .box_1>span:nth-child(1){
  36. color: #333;
  37. font-size: 17px;
  38. font-weight: 600;
  39. }
  40. .box_1>span:nth-child(2){
  41. color: #333;
  42. font-size: 15px;
  43. }
  44. .box_1>div{
  45. display: flex;
  46. justify-content: space-between;
  47. align-items: center;
  48. margin: 20px 0 0;
  49. }
  50. .box_1>div>span{
  51. color: #9B9B9B;
  52. font-size: 14px;
  53. }
  54. .box_1>div>div{
  55. display: flex;
  56. align-items: center;
  57. }
  58. .box_1>div>div>div{
  59. margin: 0 10px;
  60. font-size: 14px;
  61. padding: 4px 14px;
  62. border-radius: 2px;
  63. }
  64. .box_1>div>div>div:nth-child(1){
  65. background: #F5F5F5;
  66. color: #9B9B9B;
  67. }
  68. .box_1>div>div>div:nth-child(2){
  69. background: #E8F4FD;
  70. color:#386DB0;
  71. }
  72. .submit_btn{
  73. margin: 20px auto;
  74. padding: 8px 0px;
  75. text-align: center;
  76. font-size: 14px;
  77. background: #1386F6;
  78. border-radius: 3px;
  79. color: #fff;
  80. width: 100px;
  81. box-shadow: -1px 2px 4px #C2C2C2;
  82. position:relative;
  83. overflow: hidden;
  84. cursor: pointer;
  85. }
  86. /* .submit_btn:hover span{
  87. left: 100%;
  88. transition: all .5s;
  89. } */
  90. .go{
  91. left: 100% !important;
  92. transition: all .5s;
  93. }
  94. .submit_btn span{
  95. position: absolute;
  96. top:0;
  97. bottom: 0;
  98. left: 0;
  99. width: 100px;
  100. background-image: linear-gradient(0deg,rgba(255,255,255,0),rgba(255,255,255,0.3),rgba(255,255,255,0));
  101. left: -100%;
  102. transform: rotate(-50deg);
  103. }
  104. </style>
  105. </head>
  106. <body>
  107. <div class="big-box">
  108. </div>
  109. <div class="submit_btn">提交<span></span></div>
  110. <script>
  111. let dataList = [ //第一次请求的数据
  112. {
  113. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  114. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  115. guanzhu:'12',
  116. },
  117. {
  118. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  119. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  120. guanzhu:'12',
  121. },
  122. {
  123. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  124. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  125. guanzhu:'12',
  126. },
  127. {
  128. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  129. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  130. guanzhu:'12',
  131. },
  132. {
  133. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  134. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  135. guanzhu:'12',
  136. },
  137. {
  138. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  139. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  140. guanzhu:'12',
  141. }
  142. ];
  143. let box_1 = document.getElementsByClassName('box_1'); //获取dom
  144. let html = '',index = 0,z_index = dataList.length;
  145. for(let item of dataList){
  146. index++,z_index--;
  147. html+=`<div class="box_1 boxs_${index}" style="z-index: ${z_index}; opacity: ${index>2?0:1}; ${index == 2?'top: 22px;; transform: scale(.96);':''} ">
  148. <span>${item.title1}</span>
  149. <span>${item.title2}</span>
  150. <div>
  151. <span>${item.guanzhu}人关注</span>
  152. <div>
  153. <div>忽略</div>
  154. <div>回答</div>
  155. </div>
  156. </div>
  157. </div>`
  158. }
  159. document.getElementsByClassName('big-box')[0].innerHTML = html;
  160. for(let i in dataList){
  161. i++
  162. let doc = document.getElementsByClassName('boxs_'+i)[0]
  163. touchevent(doc,i) //注入滑动事件
  164. }
  165. function touchevent(target,i){
  166. let pageX,startX,offsetLeft,lefts,endpageX;
  167. return target.addEventListener('touchstart',function(e){
  168. console.log(target.previousSibling)
  169. if(target.previousSibling){
  170. if(target.previousSibling.style.opacity == 1) return;
  171. }
  172. target.style.cssText = 'z-index:999; opacity:1;'
  173. startX = e.targetTouches[0].pageX;
  174. offsetLeft = target.offsetLeft;
  175. }),target.addEventListener('touchmove',function(e){
  176. if(target.previousSibling){
  177. if(target.previousSibling.style.opacity == 1) return;
  178. }
  179. e.preventDefault()
  180. pageX = e.targetTouches[0].pageX;
  181. let left = pageX - startX + parseInt(offsetLeft)
  182. lefts = left
  183. target.style.cssText = 'z-index:999; left:'+left+'px; opacity:1;'
  184. }),target.addEventListener('touchend',function(e){
  185. if(target.previousSibling){
  186. if(target.previousSibling.style.opacity == 1) return;
  187. }
  188. if(i == dataList.length)lefts = 0;
  189. endpageX = e.changedTouches[0].pageX;
  190. if(endpageX === startX)return;
  191. if(lefts<=-70){
  192. target.style.cssText = 'left: -110%; transition:all .3s; z-index:999; opacity:0;'
  193. next(target,i);
  194. }else if(lefts>=70){
  195. target.style.cssText = 'left: 110%; transition:all .3s; z-index:999; opacity:0;'
  196. next(target,i);
  197. }else{
  198. target.style.cssText = 'left: '+offsetLeft+'px; transition:all .3s; z-index:999; opacity:1;'
  199. setTimeout(() => {
  200. target.style.cssText = 'left: '+offsetLeft+'px; z-index:999; opacity:1;';
  201. }, 300);
  202. }
  203. })
  204. };
  205. function next(target,i){
  206. if(i == dataList.length -2){ console.log('执行下一页');page() }
  207. i++;
  208. document.getElementsByClassName('boxs_'+i)[0].style.cssText = 'opacity: 1; transform: scale(1); transition:all .3s; z-index:999; top:10px;'
  209. i++;
  210. document.getElementsByClassName('boxs_'+i)[0].style.cssText = 'opacity: 1; transform: scale(.96); transition:all .3s; z-index:99; top:22px;'
  211. }
  212. function page(){
  213. let pageList = [ //请求下一页的数据
  214. {
  215. title1:'我是下一页',
  216. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  217. guanzhu:'12',
  218. },
  219. {
  220. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  221. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  222. guanzhu:'12',
  223. },
  224. {
  225. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  226. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  227. guanzhu:'12',
  228. },
  229. {
  230. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  231. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  232. guanzhu:'12',
  233. },
  234. {
  235. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  236. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  237. guanzhu:'12',
  238. },
  239. {
  240. title1:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  241. title2:'送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发送积分哦啊还发我我都是佛我爱上覅都是佛我爱上覅发',
  242. guanzhu:'12',
  243. }
  244. ];
  245. dataList.push(...pageList)
  246. let html = '',z_index = dataList.length;
  247. for(let item of pageList){
  248. index++,z_index--;
  249. html+=`<div class="box_1 boxs_${index}" style="z-index: ${z_index}; opacity: 0; ${index == 2?'top: 22px;; transform: scale(.96);':''} ">
  250. <span>${item.title1}</span>
  251. <span>${item.title2}</span>
  252. <div>
  253. <span>${item.guanzhu}人关注</span>
  254. <div>
  255. <div>忽略</div>
  256. <div>回答</div>
  257. </div>
  258. </div>
  259. </div>`
  260. }
  261. $(".big-box").append(html)
  262. let indexs = index - pageList.length
  263. for(let i in pageList){
  264. indexs++
  265. let doc = document.getElementsByClassName('boxs_'+indexs)[0]
  266. touchevent(doc,indexs)
  267. }
  268. }
  269. let btn = document.getElementsByClassName('submit_btn')[0];
  270. btn.onclick = function(e){
  271. this.children[0].classList.add('go')
  272. console.log(this.children)
  273. setTimeout(() => {
  274. this.children[0].classList.remove('go')
  275. }, 500);
  276. }
  277. </script>
  278. </body>
  279. </html>
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/136
0 评论
2.7k

发表评论

!