亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 7
粉絲 0
評論 0
訪問量 3450
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
仿寫課堂的輪播圖案例
P粉276126820
原創(chuàng)
440人瀏覽過

輪播圖

html

  1. <html lang="zh-CN">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <link rel="stylesheet" href="./style.css">
  8. </head>
  9. <body>
  10. <div class="slideshow">
  11. <div class="imgs">
  12. </div>
  13. <div class="btns">
  14. </div>
  15. </div>
  16. <script type="module">
  17. import slide from './js/slideshow.js'
  18. const imgs = document.querySelector('.imgs')
  19. const btns = document.querySelector('.btns')
  20. window.onload= function(){
  21. //創(chuàng)建圖片
  22. slide.createImgs(imgs)
  23. //創(chuàng)建按鈕
  24. slide.createButts(imgs,btns)
  25. //為每個按鈕添加點擊事件
  26. ;[...btns.children].forEach(btn=>{
  27. btn.onclick = function(){
  28. slide.switchImg(btn,imgs)
  29. }
  30. }
  31. )
  32. // 間歇式定時器,第2秒換一張圖片
  33. setInterval(
  34. function (btnsArr,btnKeys){
  35. slide.timePlay(btnsArr,btnKeys)
  36. },
  37. 3000
  38. ,
  39. //獲取按鈕組
  40. [...btns.children]
  41. ,
  42. //獲取按鈕組鍵值
  43. Object.keys([...btns.children])
  44. )
  45. }
  46. </script>
  47. </body>
  48. </html>

JS

  1. export default [
  2. {
  3. key: 1,
  4. src: 'images/item1.jpeg',
  5. url: 'http://ipnx.cn',
  6. },
  7. {
  8. key: 2,
  9. src: 'images/item2.jpeg',
  10. url: 'http://ipnx.cn',
  11. },
  12. {
  13. key: 3,
  14. src: 'images/item3.jpeg',
  15. url: 'http://ipnx.cn',
  16. },
  17. ]
  18. ------------------------------------------------------
  19. import imgArr from './data.js'
  20. /**
  21. * 創(chuàng)建圖片元素
  22. * @param {DOMElement} imgs --圖片容器
  23. */
  24. function createImgs(imgs){
  25. const frag = new DocumentFragment()
  26. for(let i=0 ; i<imgArr.length ; i++){
  27. const img =document.createElement('img')
  28. img.src=imgArr[i].src
  29. img.dataset.key = imgArr[i].key
  30. if(i===0)img.classList.add('active')
  31. img.onclick = () => (location.href = imgArr[i].url)
  32. frag.append(img)
  33. }
  34. imgs.append(frag)
  35. }
  36. /**
  37. * @desc 創(chuàng)建按鈕組
  38. * @param {DOMElement} imgs - 圖片容器
  39. * @param {DOMElement} btns - 按鈕容器
  40. */
  41. function createButts(imgs,btns){
  42. //獲取圖片數(shù)量
  43. let length = imgs.childElementCount
  44. console.log(length);
  45. for(let i=0 ; i<length; i++){
  46. const btn = document.createElement('span')
  47. btn.dataset.key = imgs.children[i].dataset.key
  48. if(i===0)btn.classList.add('active')
  49. btns.append(btn)
  50. }
  51. }
  52. /**
  53. * @desc 創(chuàng)建按鈕事件
  54. * @param {DOMElement} btn - 當(dāng)前按鈕
  55. * @param {DOMElement} imgs - 圖片容器
  56. */
  57. function switchImg(btn,imgs){
  58. // 去掉所有按鈕圖片的激活狀態(tài)
  59. ;[...btn.parentNode.children].forEach(btn=>btn.classList.remove('active'))
  60. ;[...imgs.children].forEach(img=>img.classList.remove('active'))
  61. //將當(dāng)前用戶正在點擊的按鈕設(shè)置激活狀態(tài)
  62. btn.classList.add('active')
  63. //獲取當(dāng)前用戶點擊的圖片
  64. const curImg = [...imgs.children].find(img=>img.dataset.key===btn.dataset.key)
  65. //把當(dāng)前圖片設(shè)置激活狀態(tài)
  66. curImg.classList.add('active')
  67. }
  68. /**
  69. * @desc 定時輪播器: 間歇式的定時器
  70. * @param {DOMElement} btnsArr - 按鈕數(shù)組(用來綁定事件)
  71. * @param {DOMElement} btnKeys - 按鈕的鍵構(gòu)成的數(shù)組
  72. */
  73. function timePlay(btnsArr,btnKeys){
  74. let k = btnKeys.shift()
  75. // 根據(jù)索引,從按鈕組中找到與該索引對應(yīng)的按鈕,給它自動派發(fā)一個點擊事件
  76. btnsArr[k].dispatchEvent(new Event('click'))
  77. btnKeys.push(k)
  78. }
  79. export default {createImgs,createButts,switchImg,timePlay}

css

  1. body {
  2. background-color: #eee;
  3. }
  4. /* 輪播圖容器 */
  5. .slideshow {
  6. width: 240px;
  7. height: 360px;
  8. /* em / rem */
  9. }
  10. /* 圖片容器 */
  11. .slideshow .imgs {
  12. width: inherit;
  13. height: inherit;
  14. /* width: 100%;
  15. height: 100%; */
  16. }
  17. /* 圖片適應(yīng) */
  18. .slideshow img {
  19. width: 100%;
  20. height: 100%;
  21. border-radius: 10px;
  22. /* ? 默認(rèn)全隱藏 */
  23. display: none;
  24. }
  25. /* 設(shè)置圖片的激活狀態(tài) */
  26. .slideshow img.active {
  27. display: block;
  28. }
  29. .slideshow img:hover {
  30. cursor: pointer;
  31. }
  32. /* ------ 按鈕容器 ------- */
  33. /* 按鈕容器 */
  34. .slideshow .btns {
  35. display: flex;
  36. place-content: center;
  37. position: relative;
  38. top: -40px;
  39. /* transform: translateY(-40px); */
  40. }
  41. .slideshow .btns > span {
  42. background-color: rgba(48, 148, 48, 0.8);
  43. height: 16px;
  44. width: 16px;
  45. border-radius: 50%;
  46. margin: 5px;
  47. }
  48. .slideshow .btns > span.active {
  49. background-color:crimson;
  50. }
  51. .slideshow .btns > span:hover {
  52. cursor: pointer;
  53. }

效果

批改老師:PHPzPHPz

批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費學(xué)