1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>BottomNavigationMenu</title> <link rel="stylesheet/less" type="text/css" href="./style.less" /> <script src="https://cdn.jsdelivr.net/npm/less@4.1.2/dist/less.min.js" ></script> </head> <body> <div class="menu-box"> <div class="menu-item active"> <i class="menu-icon home"></i> <span>首页</span> </div> <div class="menu-item"> <i class="menu-icon discover"></i> <span>发现</span> </div> <div class="menu-item"> <i class="menu-icon search"></i> <span>探索</span> </div> <div class="menu-item"> <i class="menu-icon my"></i> <span>我的</span> </div> <div class="menu-indicator"></div> </div> <script> let current = 0 const menus = document.getElementsByClassName('menu-item') for (let i = 0; i < menus.length; i++) { const menu = menus[i] menu.addEventListener('click', e => { e.preventDefault() changeTab(i) }) } function changeTab(index) { if (index === current) { return } const menus = document.getElementsByClassName('menu-item') if (!menus.length) { return } for (let i = 0; i < menus.length; i++ ) { if (i === index) { menus[i].classList.add('active') setKeyframes(`move${index + 1}`, current, index) document.getElementsByClassName('menu-indicator')[0].style.animation = `move${index + 1} 1s` }else { menus[i].classList.remove('active') } } current = index } function setKeyframes(key_name, current, index) { const token = window.WebKitCSSKeyframesRule ? '-webkit-':''; const nameRule = getKeyframes(key_name); let rules = ` @${token}keyframes ${key_name}{ 0% { left: ${12.5 + 25 * current}% } 100% { left: ${12.5 + 25 * index}%; } } ` if(JSON.stringify(nameRule) == '{}'){ document.styleSheets[0].insertRule(rules,0); }else{ nameRule.styleSheet.deleteRule(nameRule.index) nameRule.styleSheet.insertRule(rules,nameRule.index) } } function getKeyframes(name){ const animation={} const styleSheets=document.styleSheets for(let i=0;i<styleSheets.length;i++){ const item = styleSheets[i]; for (let j = 0; j < item.cssRules.length; j ++) { if(item.cssRules[j] && item.cssRules[j].name && item.cssRules[j].name == name){ animation.cssRule = item.cssRules[j]; animation.styleSheet = item; animation.index = j; } } } return animation; } </script> </body> </html>
|