10
0
fengchuanyu 8 сар өмнө
parent
commit
6710b2cdde

+ 127 - 38
8_移动端/练习题4_猫眼电影移动端.html

@@ -1,49 +1,63 @@
 <!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{
+        body {
             margin: 0;
         }
-        ul{
-            margin:0;
-            padding:0;
+
+        ul {
+            margin: 0;
+            padding: 0;
         }
-        li{
+
+        li {
             list-style: none;
         }
-        .container{
+
+        .container {
             font-size: .36rem;
         }
+
         /* 头部  */
-        .top-head header{
+        .top-head header {
             background-color: #e54847;
             height: 0.7rem;
             text-align: center;
             line-height: 0.7rem;
             position: relative;
-        } 
-        .top-head header span{
+        }
+
+        .top-head header span {
             color: #fff;
         }
-        .top-head .more-btn{
+
+        .top-head .more-btn {
             position: absolute;
             right: 20px;
-            top:0;
+            top: 0;
         }
-        .top-head .more-btn ul{
+
+        .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);
+            box-shadow: 0 .06rem .18rem 0 rgba(0, 0, 0, .1);
         }
-        .top-head .more-btn ul li{
+
+        .top-head .more-btn .active {
+            display: block;
+        }
+
+        .top-head .more-btn ul li {
             height: .9rem;
             line-height: .9rem;
             font-size: 0.3rem;
@@ -52,12 +66,14 @@
             background-color: #fff;
             border-bottom: .02rem solid #f0f0f0;
         }
-        .top-head .more-btn img{
+
+        .top-head .more-btn img {
             width: .3rem;
             height: .3rem;
         }
+
         /* 头部导航 */
-        .top-head nav{
+        .top-head nav {
             height: .9rem;
             border-bottom: 0.02rem solid #e6e6e6;
             display: flex;
@@ -65,12 +81,14 @@
             padding: 0 .2rem;
             align-items: center;
         }
-        .top-head .nav-center{
-            flex-grow:3;
+
+        .top-head .nav-center {
+            flex-grow: 3;
             margin: 0 .2rem;
             position: relative;
         }
-        .top-head .nav-center ul{
+
+        .top-head .nav-center ul {
             display: flex;
             justify-content: space-around;
             font-size: .3rem;
@@ -78,56 +96,79 @@
             color: #666;
             align-items: center;
         }
-        .top-head .nav-center .bottom-line{
+
+        .top-head .nav-center .bottom-line {
             position: absolute;
             bottom: -5px;
-            left: 4vw;
             width: .4rem;
             height: 3px;
             border-radius: 3px;
             background-color: #f03d37;
+            transition: left .1s linear;
         }
 
-        .top-head .nav-center .active{
+        .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{
+
+        .top-head .nav-right {
             flex-grow: 1;
             display: flex;
             align-items: center;
             justify-content: center;
         }
-        .top-head .nav-right img{
+
+        .top-head .nav-right img {
             width: .4rem;
             height: .4rem;
         }
-        .top-head .nav-left{
+
+        .top-head .nav-left {
             display: flex;
             align-items: center;
-            
+
         }
-        .top-head .nav-left span{
+
+        .top-head .nav-left span {
             color: #666;
             font-size: .3rem;
         }
-        .top-head .nav-left i{
-            border-top:5px solid #666;
+
+        .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);
+            transform: translate(3px, 3px);
         }
     </style>
 </head>
+
 <body>
     <div class="container">
         <div class="top-head">
             <header>
                 <span>猫眼电影</span>
-                <div class="more-btn">   
-                    <img src="./img/list.png" alt="list">
-                    <ul>
+                <div class="more-btn">
+                    <img id="list-btn" src="./img/list.png" alt="list">
+                    <ul class="">
                         <li>首页</li>
                         <li>榜单</li>
                         <li>热点</li>
@@ -140,12 +181,12 @@
                     <span>哈尔滨</span>
                     <i></i>
                 </div>
-                <div class="nav-center">
+                <div class="nav-center" data-tab-val="one">
                     <ul>
-                        <li class="active">热播</li>
-                        <li>影院</li>
-                        <li>待映</li>
-                        <li>经典电影</li>
+                        <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>
@@ -157,5 +198,53 @@
         <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>