生活随笔
收集整理的這篇文章主要介紹了
原生JS实现移动端上下滑动一次一屏(仿抖音)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
功能如下:
- 頭部: 附近、關注、推薦選項卡的切換
- 左右滑動功能、頭部選項卡跟隨動畫
- 上下滑動劃動一屏,滑動超過頭部刷新
- 雙擊選項卡回到頂部
上代碼:
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document
</title><style>* {margin: 0;padding: 0;-moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;-khtml-user-select: none;user-select: none;}#box {width: 350px;height: 500px;margin: 30px auto;border-radius: 5px;box-shadow: 0px 0px 27px -3px red;-webkit-border-radius: 5px;-moz-border-radius: 5px;-ms-border-radius: 5px;-o-border-radius: 5px;overflow: hidden;position: relative;background-color: #ccc;}.childbox {width: 300%;height: 100%;display: flex;position: absolute;top: 0;left: 0;}.childbox>div {flex: 1;height: 100%;}.child1 {background-color: salmon;}.child2 {background-color: greenyellow;}.child3 {background-color: blueviolet;}.nav_box {position: absolute;width: 100%;text-align: center;line-height: 50px;}.nav_box div {display: inline-block;color: #fff;margin: 0 5px;position: relative;}.active_nav::before {content: '';position: absolute;background-color: #fff;left: 2px;bottom: 7px;width: 27px;height: 2px;}.childbox>div {position: relative;}.childbox>div .listview {width: 100%;position: absolute;}.view_child {text-align: center;line-height: 200px;color: #fff;font-size: 25px;}</style>
</head><body><div id="box"><div class="childbox"><div class="child1"><div class="listview" type="附近"></div></div><div class="child2"><div class="listview" type="關注"></div></div><div class="child3"><div class="listview" type="推薦"></div></div></div><div class="nav_box"><div>附近
</div><div>關注
</div><div class="active_nav">推薦
</div></div></div>
</body>
<script>let childbox = document.querySelector('.childbox')let viewheight = document.querySelector('#box').offsetHeightlet childnav = document.querySelector('.nav_box').querySelectorAll('div')let viewlist = document.querySelectorAll('.listview')let indextype = 2let view_index = {0: [0, 0, 1],1: [0, 0, 1],2: [0, 0, 1]}set_nav_active(indextype)function set_nav_active(index) {for (let i = 0; i < childnav.length; i++) {childnav[i].className = ''}childnav[index].className = 'active_nav'childbox.style.left = index * -100 + '%'}for (let i = 0; i < childnav.length; i++) {childnav[i].onclick = function () {childbox.style.transition = 'all 0.5s'indextype = iset_nav_active(indextype)}}let box = document.querySelector('#box')let transition_status = truebox.onmousedown = function (event) {if (!transition_status) {return}let startY = event.clientYlet startX = event.clientXlet t_l_type = truelet move_type = 0let transition_type = 0childbox.style.transition = ''let startleft = childbox.offsetLeftlet type = {a: false,b: ''}let startTop = viewlist[indextype].offsetToplet top_type_view = {a: false, b: '', }console.log(startTop)viewlist[indextype].style.transition = '';let b_top = 0document.onmousemove = function (event) {let moveY = event.clientYlet moveX = event.clientXif (t_l_type) {if (Math.abs(moveY - startY) > 5) {t_l_type = falsemove_type = 2}if (Math.abs(moveX - startX) > 5) {t_l_type = falsemove_type = 1}}if (move_type == 2) {if (view_index[indextype][0] == 0 && moveY - startY > 0) {console.log('下拉')transition_type = 1b_top = moveY - startYviewlist[indextype].style.top = b_top + 'px'return}if (transition_type != 1) {transition_type = 2let moveY = event.clientYlet num = moveY - startYviewlist[indextype].style.top = startTop + num + 'px'if (num > 70) {top_type_view.a = truetop_type_view.b = '上'} else if (num < -70) {top_type_view.a = truetop_type_view.b = '下'}}} else if (move_type == 1) {transition_type = 3let moveX = event.clientXlet num = moveX - startXchildbox.style.left = startleft + num + 'px'if (moveX > startX) {if (num > 100) {type.a = truetype.b = '右'}} else if (moveX < startX) {if (num < -100) {type.a = truetype.b = '左'}}}}window.onmouseup = function () {document.onmousemove = ''if (transition_type == 1) {viewlist[indextype].style.transition = 'all .5s';if (b_top > 70) {transition_status = falseviewlist[indextype].style.top = '70px'setTimeout(function () {viewlist[indextype].style.top = '0px'view_index[indextype][2] = 1autoview(indextype)setTimeout(() => {transition_status = true}, 500);}, 2000)} else {viewlist[indextype].style.top = '0px'}} else if (transition_type == 2) {viewlist[indextype].style.transition = 'all .5s';if (top_type_view.a) {if (top_type_view.b == '上') {view_index[indextype][0]--if (view_index[indextype][0] <= -1) {view_index[indextype][0] = 0}viewlist[indextype].style.top = view_index[indextype][0] * -viewheight + 'px'console.log('上')} else if (top_type_view.b == '下') {view_index[indextype][0]++if (view_index[indextype][0] >= view_index[indextype][1] - 2) {autoview(indextype)}viewlist[indextype].style.top = view_index[indextype][0] * -viewheight + 'px'}} else {viewlist[indextype].style.top = startTop + 'px'}} else if (transition_type == 3) {if (type.a) {if (type.b === '左') {indextype++if (indextype >= 3) {indextype = 2}} else if (type.b === '右') {indextype--if (indextype <= -1) {indextype = 0}}}childbox.style.transition = 'all 0.5s'set_nav_active(indextype)}t_l_type = truemove_type = 0transition_type = 0}}function autocolor() {return `rgb(${Math.floor(Math.random() * 255)},${Math.floor(Math.random() * 255)},${Math.floor(Math.random() * 255)})`}for (let i = 0; i < viewlist.length; i++) {autoview(i)}function autoview(index) {let type = viewlist[index].getAttribute('type')if (view_index[index][2] == 1) {viewlist[indextype].innerHTML = ''view_index[index][1] = 10} else {view_index[index][1] += 10}for (let i = 0; i < 10; i++) {let div = document.createElement('div')div.className = 'view_child'div.innerHTML = `<div>${type}:${(view_index[index][2] - 1) * 10 + i + 1}</div><hr></hr><div>頁碼:${view_index[index][2]}</div>`div.style.backgroundColor = autocolor()div.style.height = viewheight + 'px'viewlist[index].appendChild(div)}view_index[index][2]++console.log(view_index)}let nav_box = document.querySelector('.nav_box')nav_box.ondblclick = function () {viewlist[indextype].style.transition = 'all .5s'viewlist[indextype].style.top = '0px'view_index[indextype][0] = 0}
</script></html>
最后祝大家端午安康~,記得一件三連!!!!
總結
以上是生活随笔為你收集整理的原生JS实现移动端上下滑动一次一屏(仿抖音)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。