首页 > 其他分享 >js hw12

js hw12

时间:2023-01-02 21:00:54浏览次数:44  
标签:hw12 classList smalls js width length let big

  1. 标签页效果-鼠标滑过高亮的菜单选择效果
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    background-color: black;
  }

  .example {
    width: 300px;
    height: 200px;
    background-color: pink;
    margin: 200px auto;
    border-radius: 10px;
  }

  ul {
    background-color: green;
    display: flex;
    border: 1px solid green;
    border-radius: 10px;
  }

  li {
    text-align: center;
    background-color: gray;
    flex: 1;
    list-style: none;
    border: 1px solid green;
    border-radius: 10px;
  }

  .content {
    background-color: pink;
    width: 300px;
  }

  .box {
    background-color: pink;
    display: none;
  }

  .active {
    display: block;
  }

  .high {
    background-color: yellow;
  }
</style>
</head>

<body>
<div class="example">
  <ul>
    <li class="high">a</li>
    <li>b</li>
    <li>c</li>
  </ul>
  <div class="content">
    <div class="active box">a</div>
    <div class="box">b</div>
    <div class="box">c</div>
  </div>
</div>
<script>
  ; (function () {
    lis = document.querySelectorAll("ul li");
    boxs = document.querySelectorAll(".box");
    //console.log(boxs);
    lis.forEach((li, index) => {
      li.addEventListener('mouseover', function (e) {
        ul = e.target.parentElement
        let ans = -1;
        for (let i = 0; i < ul.children.length; i++) {
          if (ul.children[i].classList.contains("high")) {
            ul.children[i].classList.remove("high")
            boxs[i].classList.remove("active")
          }
          if (e.target === ul.children[i]) {
            ans = i;
          }
        }
        boxs[ans].classList.add("active")
        e.target.classList.add("high")
      })
    })
  }())
</script>
</body>

</html>
  1. 点击切换图片的相册
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    .bigBox {
      position: relative;
      margin: 10px auto;
      width: 1000px;
    }

    .big {
      width: 1000px;
      height: 562.5px;
      opacity: 100%;
      transition: opacity 1s;
    }

    .smallBox {
      margin: 10px auto;
      width: 400px;
      font-size: 0;
    }

    .small {
      width: 100px;
      height: 56.25px;
    }

    .toggle {
      opacity: 0%;
    }
  </style>
  </style>
</head>

<body>
  <div class="bigBox">
    <img src="./pic/1.png" class="big">
  </div>
  <div class="smallBox">
    <img src="./pic/1.png" class="small">
    <img src="./pic/2.png" class="small">
    <img src="./pic/3.png" class="small">
    <img src="./pic/4.png" class="small">
  </div>
  <script>
    big = document.querySelector(".bigBox .big")
    smalls = document.querySelectorAll(".smallBox .small")

    smalls.forEach(small => {
      small.addEventListener("click", function (e) {
        url = e.target.src
        big.classList.add("toggle")
        setTimeout(() => {
          big.src = url
          big.classList.remove("toggle")
        }, 1000)
      })
    })
  </script>
</body>

</html>
  1. 轮播图
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    .bigBox {
      position: relative;
      margin: 10px auto;
      width: 1000px;
    }

    button {
      position: absolute;
      width: 60px;
      height: 40px;
      font-size: 20px;
      text-align: center;
    }

    .pre {
      top: 50%;
      right: 100%;
    }

    .next {
      top: 50%;
    }

    .big {
      width: 1000px;
      height: 562.5px;
      opacity: 100%;
      transition: opacity 0.5s;
    }

    .smallBox {
      margin: 10px auto;
      width: 400px;
      font-size: 0;
    }

    .small {
      width: 100px;
      height: 56.25px;
    }

    .toggle {
      opacity: 0%;
    }

    .now {
      border: 1px solid yellow;
    }
  </style>
</head>

<body>

  <div class="bigBox">
    <button class="pre">pre</button>
    <img src="./pic/1.png" class="big">
    <button class="next">next</button>
  </div>
  <div class="smallBox">
    <img src="./pic/1.png" class="small now">
    <img src="./pic/2.png" class="small">
    <img src="./pic/3.png" class="small">
    <img src="./pic/4.png" class="small">
  </div>
  <script>
    pre = document.querySelector('.pre')
    next = document.querySelector('.next')
    big = document.querySelector(".bigBox .big")
    smalls = document.querySelectorAll(".smallBox .small")

    smalls.forEach(small => {
      small.addEventListener("click", function (e) {
        for (let i = 0; i < smalls.length; i++) {
          if (smalls[i].classList.contains('now')) {
            smalls[i].classList.remove('now')
          }
        }
        let url = e.target.src
        big.classList.add("toggle")
        setTimeout(() => {
          big.src = url
          big.classList.remove("toggle")
        }, 1000)
        e.target.classList.add('now')
      })
    })

    let num = -1
    let flag = false;
    pre.addEventListener("click", function (e) {
      let i = 0;
      while (1) {
        if (flag) {
          let url = smalls[(i + smalls.length - 2) % smalls.length].src
          big.classList.add("toggle")
          setTimeout(() => {
            big.src = url
            big.classList.remove("toggle")
          }, 1000)
          smalls[(i + smalls.length - 2) % smalls.length].classList.add('now')
          flag = false
          clearInterval(timer);
          timer = setInterval(donext, 4000)
          break;
        }
        if (smalls[i % smalls.length].classList.contains('now')) {
          smalls[i % smalls.length].classList.remove('now')
          num = i;
          flag = true;
        }
        i++;
      }
    })

    next.addEventListener("click", function (e) {
      let i = 0;
      while (1) {
        if (flag) {
          let url = smalls[i % smalls.length].src
          big.classList.add("toggle")
          setTimeout(() => {
            big.src = url
            big.classList.remove("toggle")
          }, 1000)
          smalls[i % smalls.length].classList.add('now')
          flag = false
          clearInterval(timer);
          timer = setInterval(donext, 4000)
          break;
        }
        if (smalls[i % smalls.length].classList.contains('now')) {
          smalls[i % smalls.length].classList.remove('now')
          num = i;
          flag = true;
        }
        i++;
      }
    })

    big.addEventListener("mouseover", function () {
      clearInterval(timer);
      timer = null;
    })

    big.addEventListener("mouseout", function () {
      timer = setInterval(donext, 4000)
    })

    function donext() {
      next.click()
    }

    let timer = setInterval(donext, 4000)
  </script>
</body>

</html>

标签:hw12,classList,smalls,js,width,length,let,big
From: https://www.cnblogs.com/echoT/p/17020498.html

相关文章

  • js hw15 spa
    spa<!--locationnavigatorhistoryscreenwindow--><!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"co......
  • 第1节 js相关概念
    1:js介绍javascript是一门弱类型的编程语言,简称js,创始人布兰登·艾奇,由美国网景公司发布。微软也有类似的语言叫jscript。前端编程语言只有js,而后端编程语言有java、C......
  • jsdemo01.html
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><scripttype="text/javascript">varx;......
  • angularJS友好URL实现 good
    nginx部署angularjs时的rewrite问题使用h5+angularjs完成了一个项目此项目在正式环境上使用nginx做webserver此项目的入口在微信/微博分享中由于分享时的项目访问地址中......
  • 前端页面HTML、JS屏蔽页面所有console.log打印日志
      //判断配置文件是否开启日志调试是否输出日志true输出false不输出varlogDebug=true;console.log=(function(oriLogFunc){retur......
  • Fastjson 专题
    ​​https://github.com/eishay/jvm-serializers/wiki​​ TypeReference1.基础使用在fastjson中提供了一个用于处理泛型反序列化的类TypeReference。importcom.alibaba.......
  • 初识JavaScript-概念,注释,js输入和输出语句
    初识JavaScript目录初识JavaScript1.初识JavaScript1.1JavaScript是什么1.1JavaScript是什么1.2JavaScript的作用1.3HTML/CSS/JS的关系1.4浏览器执行JS简介......
  • gluon 基于浏览器+nodejs 的桌面应用开发框架
    gluon基于浏览器+nodejs的桌面应用开发框架,好处是相对轻量,只使用系统浏览器,对于nodejs可以支持包含以及不包含模式目前支持chrome以及firfox浏览器器,打包的应用相对比......
  • 分享几个Ext.js 学习的地方
    分享几个Ext.js学习的地方 ​​https://www.w3cschool.cn/extjs/extjs_themes.html​​  W3C ​​http://extjs.org.cn/ ​​Sencha中文站日后还会更新!......
  • jsp自定义标签
    jsp自定义标签   需求:向浏览器输出当前客户的IP地址(只能使用jsp标签)1.自定义标签开发步骤    1. 编写一个普通的java类,继承SimpleTagSupport......