练习题4_猫眼电影移动端.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. <title>Document</title>
  7. <script src="./rem.js"></script>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. ul {
  13. margin: 0;
  14. padding: 0;
  15. }
  16. li {
  17. list-style: none;
  18. }
  19. .container {
  20. font-size: .36rem;
  21. }
  22. /* 头部 */
  23. .top-head header {
  24. background-color: #e54847;
  25. height: 0.7rem;
  26. text-align: center;
  27. line-height: 0.7rem;
  28. position: relative;
  29. }
  30. .top-head header span {
  31. color: #fff;
  32. }
  33. .top-head .more-btn {
  34. position: absolute;
  35. right: 20px;
  36. top: 0;
  37. }
  38. .top-head .more-btn ul {
  39. display: none;
  40. position: absolute;
  41. top: 0.5rem;
  42. left: -1.5rem;
  43. width: 2rem;
  44. z-index: 1;
  45. box-shadow: 0 .06rem .18rem 0 rgba(0, 0, 0, .1);
  46. }
  47. .top-head .more-btn .active {
  48. display: block;
  49. }
  50. .top-head .more-btn ul li {
  51. height: .9rem;
  52. line-height: .9rem;
  53. font-size: 0.3rem;
  54. text-align: center;
  55. color: #333;
  56. background-color: #fff;
  57. border-bottom: .02rem solid #f0f0f0;
  58. }
  59. .top-head .more-btn img {
  60. width: .3rem;
  61. height: .3rem;
  62. }
  63. /* 头部导航 */
  64. .top-head nav {
  65. height: .9rem;
  66. border-bottom: 0.02rem solid #e6e6e6;
  67. display: flex;
  68. justify-content: space-between;
  69. padding: 0 .2rem;
  70. align-items: center;
  71. }
  72. .top-head .nav-center {
  73. flex-grow: 3;
  74. margin: 0 .2rem;
  75. position: relative;
  76. }
  77. .top-head .nav-center ul {
  78. display: flex;
  79. justify-content: space-around;
  80. font-size: .3rem;
  81. font-weight: 700;
  82. color: #666;
  83. align-items: center;
  84. }
  85. .top-head .nav-center .bottom-line {
  86. position: absolute;
  87. bottom: -5px;
  88. width: .4rem;
  89. height: 3px;
  90. border-radius: 3px;
  91. background-color: #f03d37;
  92. transition: left .1s linear;
  93. }
  94. .top-head .nav-center[data-tab-val="one"] .bottom-line {
  95. left: 4vw;
  96. }
  97. .top-head .nav-center[data-tab-val="two"] .bottom-line {
  98. left: 18vw;
  99. }
  100. .top-head .nav-center[data-tab-val="three"] .bottom-line {
  101. left: 32vw;
  102. }
  103. .top-head .nav-center[data-tab-val="four"] .bottom-line {
  104. left: 49vw;
  105. }
  106. .top-head .nav-center .active {
  107. color: #333;
  108. font-size: .34rem;
  109. }
  110. .top-head .nav-right {
  111. flex-grow: 1;
  112. display: flex;
  113. align-items: center;
  114. justify-content: center;
  115. }
  116. .top-head .nav-right img {
  117. width: .4rem;
  118. height: .4rem;
  119. }
  120. .top-head .nav-left {
  121. display: flex;
  122. align-items: center;
  123. }
  124. .top-head .nav-left span {
  125. color: #666;
  126. font-size: .3rem;
  127. }
  128. .top-head .nav-left i {
  129. border-top: 5px solid #666;
  130. border-left: 5px solid transparent;
  131. border-right: 5px solid transparent;
  132. border-bottom: 5px solid transparent;
  133. transform: translate(3px, 3px);
  134. }
  135. </style>
  136. </head>
  137. <body>
  138. <div class="container">
  139. <div class="top-head">
  140. <header>
  141. <span>猫眼电影</span>
  142. <div class="more-btn">
  143. <img id="list-btn" src="./img/list.png" alt="list">
  144. <ul class="">
  145. <li>首页</li>
  146. <li>榜单</li>
  147. <li>热点</li>
  148. <li>商城</li>
  149. </ul>
  150. </div>
  151. </header>
  152. <nav>
  153. <div class="nav-left">
  154. <span>哈尔滨</span>
  155. <i></i>
  156. </div>
  157. <div class="nav-center" data-tab-val="one">
  158. <ul>
  159. <li data-tab="one" class="active">热播</li>
  160. <li data-tab="two">影院</li>
  161. <li data-tab="three">待映</li>
  162. <li data-tab="four">经典电影</li>
  163. </ul>
  164. <div class="bottom-line"></div>
  165. </div>
  166. <div class="nav-right">
  167. <img src="./img/search.png" alt="img">
  168. </div>
  169. </nav>
  170. </div>
  171. <div class="center-content"></div>
  172. <div class="bottom-nav"></div>
  173. </div>
  174. <script>
  175. let listBtn = document.querySelector("#list-btn");
  176. //控制顶部菜单栏的显示和隐藏
  177. listBtn.ontouchstart = function () {
  178. //toggle 切换 菜单 ul 上面active类 如果有就移除 如果没有就添加
  179. this.nextElementSibling.classList.toggle("active");
  180. }
  181. let navCenter = document.querySelector(".nav-center");
  182. let navList = navCenter.querySelectorAll("li");
  183. // 循环为列表按钮添加点击事件
  184. for (let i = 0; i < navList.length; i++) {
  185. navList[i].ontouchstart = function (e) {
  186. // 循环消除所有按钮的选中状态
  187. for (let j = 0; j < navList.length; j++) {
  188. navList[j].classList.remove("active")
  189. }
  190. // 为当前按钮添加选中状态
  191. this.classList.add("active");
  192. // 获取当前按钮dataset值
  193. let thisTab = e.target.dataset.tab
  194. // 为父元素添加data-tab-val值以控制下划线的移动位置
  195. navCenter.setAttribute("data-tab-val", thisTab)
  196. // 向前地址中添加选中按钮的hash值
  197. location.hash = thisTab;
  198. }
  199. }
  200. // 监听hash值变化修改选中项
  201. window.onhashchange = function () {
  202. let thisHash = location.hash.substr(1).trim();
  203. if (thisHash) {
  204. navCenter.setAttribute("data-tab-val", thisHash);
  205. for (let i = 0; i < navList.length; i++) {
  206. if (navList[i].dataset.tab == thisHash) {
  207. navList[i].classList.add("active");
  208. } else {
  209. navList[i].classList.remove("active");
  210. }
  211. }
  212. }else{
  213. navCenter.setAttribute("data-tab-val", "one");
  214. for (let i = 0; i < navList.length; i++) {
  215. navList[i].classList.remove("active");
  216. }
  217. navList[0].classList.add("active");
  218. }
  219. }
  220. </script>
  221. </body>
  222. </html>