您的当前位置:首页正文

黑马前端——days18_API

2024-11-20 来源:个人技术集锦

案例 1

页面框架文件

<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style1.css">
</head>

<body>
  <div class="slider">
    <div class="slider-wrapper">
      <img src="./images/slider01.jpg" alt="" />
    </div>
    <div class="slider-footer">
      <p>对人类来说会不会太超前了?</p>
      <ul class="slider-indicator">
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
      <div class="toggle">
        <button class="prev">&lt;</button>
        <button class="next">&gt;</button>
      </div>
    </div>
  </div>
  <script src="index1.js"></script>
</body>

</html>

格式文件

* {
  box-sizing: border-box;
}

.slider {
  width: 560px;
  height: 400px;
  overflow: hidden;
}

.slider-wrapper {
  width: 100%;
  height: 320px;
}

.slider-wrapper img {
  width: 100%;
  height: 100%;
  display: block;
}

.slider-footer {
  height: 80px;
  background-color: rgb(100, 67, 68);
  padding: 12px 12px 0 12px;
  position: relative;
}

.slider-footer .toggle {
  position: absolute;
  right: 0;
  top: 12px;
  display: flex;
}

.slider-footer .toggle button {
  margin-right: 12px;
  width: 28px;
  height: 28px;
  appearance: none;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border-radius: 4px;
  cursor: pointer;
}

.slider-footer .toggle button:hover {
  background: rgba(255, 255, 255, 0.2);
}

.slider-footer p {
  margin: 0;
  color: #fff;
  font-size: 18px;
  margin-bottom: 10px;
}

.slider-indicator {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  align-items: center;
}

.slider-indicator li {
  width: 8px;
  height: 8px;
  margin: 4px;
  border-radius: 50%;
  background: #fff;
  opacity: 0.4;
  cursor: pointer;
}

.slider-indicator li.active {
  width: 12px;
  height: 12px;
  opacity: 1;
}

事件文件

const sliderData = [
  { url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
  { url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
  { url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
  { url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
  { url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
  { url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
  { url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
  { url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
]
const img = document.querySelector('.slider-wrapper img')
const p = document.querySelector('.slider-footer p')
const footer = document.querySelector('.slider-footer')
let move = function (idx) {
  document.querySelector(`.slider-indicator .active`).classList.remove('active')
  img.src = sliderData[idx].url
  p.innerHTML = sliderData[idx].title
  footer.style.backgroundColor = sliderData[idx].color
  document.querySelector(`.slider-indicator li:nth-child(${idx + 1})`).classList.add('active')
}

let i = 0
const next = document.querySelector('.next')
next.addEventListener('click', function () {
  i = (i + 1) % sliderData.length
  move(i)
})
const prev = document.querySelector('.prev')
prev.addEventListener('click', function () {
  i = (i - 1 + sliderData.length) % sliderData.length
  move(i)
})

let timeId = setInterval(function () {
  next.click()
}, 1000)

const slider = document.querySelector('.slider')
slider.addEventListener('mouseenter', function () {
  clearInterval(timeId)
})
slider.addEventListener('mouseleave', function () {
  timeId = setInterval(function () {
    next.click()
  }, 1000)
})

案例 2

页面框架文件

<!DOCTYPE html>
<html lang="zh-CN">

<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>
  <link rel="stylesheet" href="style2.css">
</head>

<body>
  <div class="tab">
    <div class="tab-nav">
      <h3>每日特价</h3>
      <ul>
        <li><a class="active" href="javascript:;">精选</a></li>
        <li><a href="javascript:;">美食</a></li>
        <li><a href="javascript:;">百货</a></li>
        <li><a href="javascript:;">个护</a></li>
        <li><a href="javascript:;">预告</a></li>
      </ul>
    </div>
    <div class="tab-content">
      <div class="item active"><img src="./images/tab00.png" alt="" /></div>
      <div class="item"><img src="./images/tab01.png" alt="" /></div>
      <div class="item"><img src="./images/tab02.png" alt="" /></div>
      <div class="item"><img src="./images/tab03.png" alt="" /></div>
      <div class="item"><img src="./images/tab04.png" alt="" /></div>
    </div>
  </div>
  <script src="index2.js"></script>
</body>

</html>

格式文件

* {
  margin: 0;
  padding: 0;
}

.tab {
  width: 590px;
  height: 340px;
  margin: 20px;
  border: 1px solid #e4e4e4;
}

.tab-nav {
  width: 100%;
  height: 60px;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
}

.tab-nav h3 {
  font-size: 24px;
  font-weight: normal;
  margin-left: 20px;
}

.tab-nav ul {
  list-style: none;
  display: flex;
  justify-content: flex-end;
}

.tab-nav ul li {
  margin: 0 20px;
  font-size: 14px;
}

.tab-nav ul li a {
  text-decoration: none;
  border-bottom: 2px solid transparent;
  color: #333;
}

.tab-nav ul li a.active {
  border-color: #e1251b;
  color: #e1251b;
}

.tab-content {
  padding: 0 16px;
}

.tab-content .item {
  /* 默认隐藏 */
  display: none;
}

.tab-content .item.active {
  /* 选中开启显示 */
  display: block;
}

事件文件

const as = document.querySelectorAll('.tab-nav a')
for (let i = 0; i < as.length; i++) {
  as[i].addEventListener('mouseenter', function () {
    document.querySelector('.tab-nav .active').classList.remove('active')
    this.classList.add('active')

    document.querySelector('.tab-content .active').classList.remove('active')
    document.querySelector(`.tab-content .item:nth-child(${i + 1})`).classList.add('active')
  })
}

案例 3

页面框架文件

<!DOCTYPE html>
<html lang="zh-CN">

<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>
    <link rel="stylesheet" href="style3.css">
</head>

<body>
    <div id="box">
        <ul>
            <li>
                <a href="#">
                    <img src="./images/1.jpg" alt="">
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="./images/2.jpg" alt="">
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="./images/3.jpg" alt="">
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="./images/4.jpg" alt="">
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="./images/5.jpg" alt="">
                </a>
            </li>
        </ul>
    </div>
    <script src="index3.js"></script>
</body>

</html>

格式文件

ul {
  list-style: none;
}

* {
  margin: 0;
  padding: 0;
}

div {
  width: 1200px;
  height: 400px;
  margin: 50px auto;
  border: 1px solid red;
  overflow: hidden;
}

div li {
  width: 240px;
  height: 400px;
  float: left;
  transition: all 500ms;
}

div ul {
  width: 1200px;
}





事件文件

const box = document.querySelectorAll('li');
for (let i = 0; i < box.length; i++) {
  box[i].addEventListener('mouseenter', function () {
    for (let j = 0; j < box.length; j++) {
      box[j].style.width = '100px';
    }
    this.style.width = '800px'
  })

  box[i].addEventListener('mouseleave', function () {
    for (let j = 0; j < box.length; j++) {
      box[j].style.width = '240px';
    }
  })
}
显示全文