博客列表 >实例演示轮播图和选项卡

实例演示轮播图和选项卡

早晨
早晨原创
2022年07月29日 00:46:20479浏览

轮播图演示代码

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>图片轮播</title>
  8. <style>
  9. .lb-box {
  10. width: 98%;
  11. margin: auto;
  12. position: relative;
  13. overflow: hidden;
  14. }
  15. @media (max-width: 568px) {
  16. .lb-box {
  17. width: 98%;
  18. }
  19. }
  20. .lb-content {
  21. width: 100%;
  22. height: 100%;
  23. }
  24. .lb-item {
  25. width: 100%;
  26. height: 100%;
  27. display: none;
  28. position: relative;
  29. }
  30. .lb-item > a {
  31. width: 100%;
  32. height: 100%;
  33. display: block;
  34. }
  35. .lb-item > a > img {
  36. width: 100%;
  37. height: 100%;
  38. }
  39. .lb-item > a > span {
  40. width: 100%;
  41. display: block;
  42. position: absolute;
  43. bottom: 0px;
  44. padding: 15px;
  45. color: #fff;
  46. background-color: rgba(0, 0, 0, 0.7);
  47. }
  48. @media (max-width: 568px) {
  49. .lb-item > a > span {
  50. padding: 10px;
  51. }
  52. }
  53. .lb-item.active {
  54. display: block;
  55. left: 0%;
  56. }
  57. .lb-item.active.left {
  58. left: -100%;
  59. }
  60. .lb-item.active.right {
  61. left: 100%;
  62. }
  63. /* */
  64. .lb-item.next,
  65. .lb-item.prev {
  66. display: block;
  67. position: absolute;
  68. top: 0px;
  69. }
  70. .lb-item.next {
  71. left: 100%;
  72. }
  73. .lb-item.prev {
  74. left: -100%;
  75. }
  76. .lb-item.next.left,
  77. .lb-item.prev.right {
  78. left: 0%;
  79. }
  80. .lb-sign {
  81. position: absolute;
  82. bottom: 0;
  83. left: 35%;
  84. padding: 5px 3px;
  85. border-radius: 6px;
  86. list-style: none;
  87. user-select: none;
  88. background-color: rgba(0, 0, 0, 0.7);
  89. }
  90. .lb-sign li {
  91. width: 22px;
  92. height: 20px;
  93. font-size: 14px;
  94. font-weight: 500;
  95. line-height: 20px;
  96. text-align: center;
  97. float: left;
  98. color: #aaa;
  99. margin: auto 4px;
  100. border-radius: 3px;
  101. cursor: pointer;
  102. }
  103. .lb-sign li:hover {
  104. color: #fff;
  105. }
  106. .lb-sign li.active {
  107. color: #000;
  108. background-color: #ebebeb;
  109. }
  110. .lb-ctrl {
  111. position: absolute;
  112. top: 50%;
  113. transform: translateY(-50%);
  114. font-size: 50px;
  115. font-weight: 900;
  116. user-select: none;
  117. background-color: rgba(0, 0, 0, 0.7);
  118. color: #fff;
  119. border-radius: 5px;
  120. cursor: pointer;
  121. transition: all 0.1s linear;
  122. }
  123. @media (max-width: 568px) {
  124. .lb-ctrl {
  125. font-size: 30px;
  126. }
  127. }
  128. .lb-ctrl.left {
  129. left: -50px;
  130. }
  131. .lb-ctrl.right {
  132. right: -50px;
  133. }
  134. .lb-box:hover .lb-ctrl.left {
  135. left: 10px;
  136. }
  137. .lb-box:hover .lb-ctrl.right {
  138. right: 10px;
  139. }
  140. .lb-ctrl:hover {
  141. background-color: #000;
  142. }
  143. </style>
  144. <script>
  145. class Lb {
  146. constructor(options) {
  147. this.lbBox = document.getElementById(options.id);
  148. this.lbItems = this.lbBox.querySelectorAll(".lb-item");
  149. this.lbSigns = this.lbBox.querySelectorAll(".lb-sign li");
  150. this.lbCtrlL = this.lbBox.querySelectorAll(".lb-ctrl")[0];
  151. this.lbCtrlR = this.lbBox.querySelectorAll(".lb-ctrl")[1];
  152. // 当前图片索引
  153. this.curIndex = 0;
  154. // 轮播盒内图片数量
  155. this.numItems = this.lbItems.length;
  156. // 是否可以滑动
  157. this.status = true;
  158. // 轮播速度
  159. this.speed = options.speed || 600;
  160. // 等待延时
  161. this.delay = options.delay || 3000;
  162. // 轮播方向
  163. this.direction = options.direction || "left";
  164. // 是否监听键盘事件
  165. this.moniterKeyEvent = options.moniterKeyEvent || false;
  166. // 是否监听屏幕滑动事件
  167. this.moniterTouchEvent = options.moniterTouchEvent || false;
  168. this.handleEvents();
  169. this.setTransition();
  170. }
  171. // 开始轮播
  172. start() {
  173. const event = {
  174. srcElement: this.direction == "left" ? this.lbCtrlR : this.lbCtrlL,
  175. };
  176. const clickCtrl = this.clickCtrl.bind(this);
  177. // 每隔一段时间模拟点击控件
  178. this.interval = setInterval(clickCtrl, this.delay, event);
  179. }
  180. // 暂停轮播
  181. pause() {
  182. clearInterval(this.interval);
  183. }
  184. /**
  185. * 设置轮播图片的过渡属性
  186. * 在文件头内增加一个样式标签
  187. * 标签内包含轮播图的过渡属性
  188. */
  189. setTransition() {
  190. const styleElement = document.createElement("style");
  191. document.head.appendChild(styleElement);
  192. const styleRule = `.lb-item {transition: left ${this.speed}ms ease-in-out}`;
  193. styleElement.sheet.insertRule(styleRule, 0);
  194. }
  195. // 处理点击控件事件
  196. clickCtrl(event) {
  197. if (!this.status) return;
  198. this.status = false;
  199. if (event.srcElement == this.lbCtrlR) {
  200. var fromIndex = this.curIndex,
  201. toIndex = (this.curIndex + 1) % this.numItems,
  202. direction = "left";
  203. } else {
  204. var fromIndex = this.curIndex;
  205. (toIndex = (this.curIndex + this.numItems - 1) % this.numItems),
  206. (direction = "right");
  207. }
  208. this.slide(fromIndex, toIndex, direction);
  209. this.curIndex = toIndex;
  210. }
  211. // 处理点击标志事件
  212. clickSign(event) {
  213. if (!this.status) return;
  214. this.status = false;
  215. const fromIndex = this.curIndex;
  216. const toIndex = parseInt(event.srcElement.getAttribute("slide-to"));
  217. const direction = fromIndex < toIndex ? "left" : "right";
  218. this.slide(fromIndex, toIndex, direction);
  219. this.curIndex = toIndex;
  220. }
  221. // 处理各类事件
  222. handleEvents() {
  223. // 鼠标移动到轮播盒上时继续轮播
  224. this.lbBox.addEventListener("mouseleave", this.start.bind(this));
  225. // 鼠标从轮播盒上移开时暂停轮播
  226. this.lbBox.addEventListener("mouseover", this.pause.bind(this));
  227. // 点击左侧控件向右滑动图片
  228. this.lbCtrlL.addEventListener("click", this.clickCtrl.bind(this));
  229. // 点击右侧控件向左滑动图片
  230. this.lbCtrlR.addEventListener("click", this.clickCtrl.bind(this));
  231. // 点击轮播标志后滑动到对应的图片
  232. for (let i = 0; i < this.lbSigns.length; i++) {
  233. this.lbSigns[i].setAttribute("slide-to", i);
  234. this.lbSigns[i].addEventListener(
  235. "click",
  236. this.clickSign.bind(this)
  237. );
  238. }
  239. }
  240. slide(fromIndex, toIndex, direction) {
  241. this.lbSigns[fromIndex].className = "";
  242. this.lbSigns[toIndex].className = "active";
  243. setTimeout(
  244. (() => {
  245. this.lbItems[fromIndex].className = "lb-item";
  246. this.lbItems[toIndex].className = "lb-item active";
  247. this.status = true; // 设置为可以滑动
  248. }).bind(this),
  249. this.speed + 50
  250. );
  251. }
  252. }
  253. window.onload = function () {
  254. // 轮播选项
  255. const options = {
  256. id: "lb-1", // 轮播盒ID
  257. speed: 600, // 轮播速度(ms)
  258. };
  259. const lb = new Lb(options);
  260. lb.start();
  261. };
  262. </script>
  263. </head>
  264. <body>
  265. <div class="lb-box" id="lb-1">
  266. <!-- 轮播内容 -->
  267. <div class="lb-content">
  268. <div class="lb-item active">
  269. <img src="images/t1.png" alt="" style="width: 100%" />
  270. </div>
  271. <div class="lb-item">
  272. <img src="images/t2.png" alt="" style="width: 100%" />
  273. </div>
  274. <div class="lb-item">
  275. <img src="images/t3.png" alt="" style="width: 100%" />
  276. </div>
  277. <div class="lb-item">
  278. <img src="images/t4.png" alt="" style="width: 100%" />
  279. </div>
  280. </div>
  281. <!-- 轮播标志 -->
  282. <ol class="lb-sign">
  283. <li class="active">1</li>
  284. <li>2</li>
  285. <li>3</li>
  286. <li>4</li>
  287. </ol>
  288. <!-- 轮播控件 -->
  289. <div class="lb-ctrl left"></div>
  290. <div class="lb-ctrl right"></div>
  291. </div>
  292. </body>
  293. </html>

运行效果

选项卡演示代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>选项卡</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. list-style: none;
  13. }
  14. body {
  15. font-size: 12px;
  16. color: #666;
  17. text-align: left;
  18. }
  19. h1 {
  20. text-align: center;
  21. }
  22. .zzsc {
  23. width: 500px;
  24. height: 200px;
  25. margin: 100px auto;
  26. background: #f0f0f0;
  27. font-family: "微软雅黑";
  28. }
  29. .zzsc .tab {
  30. overflow: hidden;
  31. background: #ccc;
  32. }
  33. .zzsc .tab a {
  34. display: block;
  35. padding: 10px 20px;
  36. float: left;
  37. text-decoration: none;
  38. color: #333;
  39. }
  40. .zzsc .tab a:hover {
  41. background: #e64e3f;
  42. color: #fff;
  43. text-decoration: none;
  44. }
  45. .zzsc .tab a.on {
  46. background: #e64e3f;
  47. color: #fff;
  48. text-decoration: none;
  49. }
  50. .zzsc .list {
  51. display: none;
  52. }
  53. .zzsc .list.on {
  54. display: block;
  55. }
  56. /* .zzsc .list li {
  57. display: none;
  58. } */
  59. </style>
  60. </head>
  61. <body>
  62. <div class="zzsc">
  63. <!-- 标签 -->
  64. <div class="tab">
  65. <a href="" class="on" data-index="1">国际新闻</a>
  66. <a href="" data-index="2">国内新闻</a>
  67. <a href="" data-index="3">社会民生</a>
  68. <a href="" data-index="4">本地新闻</a>
  69. </div>
  70. <!-- 内容列表 -->
  71. <ul class="list on" data-index="1">
  72. <li>1.国际新闻</li>
  73. <li>2.国际新闻</li>
  74. <li>3.国际新闻</li>
  75. <li>4.国际新闻</li>
  76. </ul>
  77. <ul class="list" data-index="2">
  78. <li>1.国内新闻</li>
  79. <li>2.国内新闻</li>
  80. <li>3.国内新闻</li>
  81. <li>4.国内新闻</li>
  82. </ul>
  83. <ul class="list" data-index="3">
  84. <li>1.社会民生</li>
  85. <li>2.社会民生</li>
  86. <li>3.社会民生</li>
  87. <li>4.社会民生</li>
  88. </ul>
  89. <ul class="list" data-index="4">
  90. <li>1.本地新闻</li>
  91. <li>2.本地新闻</li>
  92. <li>3.本地新闻</li>
  93. <li>4.本地新闻</li>
  94. </ul>
  95. </div>
  96. <script>
  97. const tab = document.querySelector(".tab");
  98. // console.log(tab);
  99. tab.addEventListener("click", show, false);
  100. tab.addEventListener("mouseover", show, false);
  101. function show() {
  102. event.preventDefault();
  103. const btns = [...event.currentTarget.children];
  104. // console.log(btns);
  105. const on = document.querySelectorAll(".list");
  106. // console.log(on);
  107. btns.forEach((item) => item.classList.remove("on"));
  108. event.target.classList.add("on");
  109. on.forEach((item) => item.classList.remove("on"));
  110. [...on]
  111. .find((list) => list.dataset.index === event.target.dataset.index)
  112. .classList.add("on");
  113. }
  114. </script>
  115. </body>
  116. </html>

运行效果

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议