123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script src="./rem.js"></script>
- <style>
- body {
- margin: 0;
- }
- ul {
- margin: 0;
- padding: 0;
- }
- li {
- list-style: none;
- }
- .container {
- font-size: .36rem;
- }
- /* 头部 */
- .top-head header {
- background-color: #e54847;
- height: 0.7rem;
- text-align: center;
- line-height: 0.7rem;
- position: relative;
- }
- .top-head header span {
- color: #fff;
- }
- .top-head .more-btn {
- position: absolute;
- right: 20px;
- top: 0;
- }
- .top-head .more-btn ul {
- display: none;
- position: absolute;
- top: 0.5rem;
- left: -1.5rem;
- width: 2rem;
- z-index: 1;
- box-shadow: 0 .06rem .18rem 0 rgba(0, 0, 0, .1);
- }
- .top-head .more-btn .active {
- display: block;
- }
- .top-head .more-btn ul li {
- height: .9rem;
- line-height: .9rem;
- font-size: 0.3rem;
- text-align: center;
- color: #333;
- background-color: #fff;
- border-bottom: .02rem solid #f0f0f0;
- }
- .top-head .more-btn img {
- width: .3rem;
- height: .3rem;
- }
- /* 头部导航 */
- .top-head nav {
- height: .9rem;
- border-bottom: 0.02rem solid #e6e6e6;
- display: flex;
- justify-content: space-between;
- padding: 0 .2rem;
- align-items: center;
- }
- .top-head .nav-center {
- flex-grow: 3;
- margin: 0 .2rem;
- position: relative;
- }
- .top-head .nav-center ul {
- display: flex;
- justify-content: space-around;
- font-size: .3rem;
- font-weight: 700;
- color: #666;
- align-items: center;
- }
- .top-head .nav-center .bottom-line {
- position: absolute;
- bottom: -5px;
- width: .4rem;
- height: 3px;
- border-radius: 3px;
- background-color: #f03d37;
- transition: left .1s linear;
- }
- .top-head .nav-center[data-tab-val="one"] .bottom-line {
- left: 4vw;
- }
- .top-head .nav-center[data-tab-val="two"] .bottom-line {
- left: 18vw;
- }
- .top-head .nav-center[data-tab-val="three"] .bottom-line {
- left: 32vw;
- }
- .top-head .nav-center[data-tab-val="four"] .bottom-line {
- left: 49vw;
- }
- .top-head .nav-center .active {
- color: #333;
- font-size: .34rem;
- }
- .top-head .nav-right {
- flex-grow: 1;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .top-head .nav-right img {
- width: .4rem;
- height: .4rem;
- }
- .top-head .nav-left {
- display: flex;
- align-items: center;
- }
- .top-head .nav-left span {
- color: #666;
- font-size: .3rem;
- }
- .top-head .nav-left i {
- border-top: 5px solid #666;
- border-left: 5px solid transparent;
- border-right: 5px solid transparent;
- border-bottom: 5px solid transparent;
- transform: translate(3px, 3px);
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="top-head">
- <header>
- <span>猫眼电影</span>
- <div class="more-btn">
- <img id="list-btn" src="./img/list.png" alt="list">
- <ul class="">
- <li>首页</li>
- <li>榜单</li>
- <li>热点</li>
- <li>商城</li>
- </ul>
- </div>
- </header>
- <nav>
- <div class="nav-left">
- <span>哈尔滨</span>
- <i></i>
- </div>
- <div class="nav-center" data-tab-val="one">
- <ul>
- <li data-tab="one" class="active">热播</li>
- <li data-tab="two">影院</li>
- <li data-tab="three">待映</li>
- <li data-tab="four">经典电影</li>
- </ul>
- <div class="bottom-line"></div>
- </div>
- <div class="nav-right">
- <img src="./img/search.png" alt="img">
- </div>
- </nav>
- </div>
- <div class="center-content"></div>
- <div class="bottom-nav"></div>
- </div>
- <script>
- let listBtn = document.querySelector("#list-btn");
- //控制顶部菜单栏的显示和隐藏
- listBtn.ontouchstart = function () {
- //toggle 切换 菜单 ul 上面active类 如果有就移除 如果没有就添加
- this.nextElementSibling.classList.toggle("active");
- }
- let navCenter = document.querySelector(".nav-center");
- let navList = navCenter.querySelectorAll("li");
- // 循环为列表按钮添加点击事件
- for (let i = 0; i < navList.length; i++) {
- navList[i].ontouchstart = function (e) {
- // 循环消除所有按钮的选中状态
- for (let j = 0; j < navList.length; j++) {
- navList[j].classList.remove("active")
- }
- // 为当前按钮添加选中状态
- this.classList.add("active");
- // 获取当前按钮dataset值
- let thisTab = e.target.dataset.tab
- // 为父元素添加data-tab-val值以控制下划线的移动位置
- navCenter.setAttribute("data-tab-val", thisTab)
- // 向前地址中添加选中按钮的hash值
- location.hash = thisTab;
- }
- }
- // 监听hash值变化修改选中项
- window.onhashchange = function () {
- let thisHash = location.hash.substr(1).trim();
- if (thisHash) {
- navCenter.setAttribute("data-tab-val", thisHash);
- for (let i = 0; i < navList.length; i++) {
- if (navList[i].dataset.tab == thisHash) {
- navList[i].classList.add("active");
- } else {
- navList[i].classList.remove("active");
- }
- }
- }else{
- navCenter.setAttribute("data-tab-val", "one");
- for (let i = 0; i < navList.length; i++) {
- navList[i].classList.remove("active");
- }
- navList[0].classList.add("active");
- }
- }
- </script>
- </body>
- </html>
|