原汁原味,纯原生js实现轮播图

方法一:

1.HTML

<div class="Rotation">
    <div class="Rotation-box">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <div class="prePage"><</div>
        <div class="nextPage">></div>
        <div class="circle-box"></div>
    </div>
</div>

2.CSS

.Rotation{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 400px;
}
.Rotation-box{
    position: relative;
    width:600px;
    height: 400px;
    overflow: hidden
}
.Rotation-box ul{
    display: flex;
    align-items: center;
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    padding: 0;
}
.Rotation-box ul li{
    width:600px;
    height: 400px;
    list-style-type: none;
    cursor: pointer;
    text-align: center;
    line-height: 400px;
    font-size: 30px;
    color: #fff;
    font-weight: 600;
}
.Rotation-box ul li:nth-child(odd){
    background: #17C694;
}
.Rotation-box ul li:nth-child(even){
    background: #F86000;
}
.prePage{
    display: none;
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    width: 30px;
    height: 50px;
    background: rgba(0, 0, 0, 0.3);
    cursor: pointer;
    text-align: center;
    line-height: 50px;
    color: #fff;
}
.nextPage{
    display: none;
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    width: 30px;
    height: 50px;
    background: rgba(0, 0, 0, 0.3);
    cursor: pointer;
    text-align: center;
    line-height: 50px;
    color: #fff;
}
.circle-box{
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
}
.circle-box span{
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.5);
    cursor: pointer;
    margin: 0 2px;
}
.current{
    display: inline-block;
    width: 15px;
    height: 15px;
    background: #fff;
}

3.js

let ul = document.querySelector('ul')
let nextPage = document.querySelector('.nextPage')
let prePage = document.querySelector('.prePage')
let circleBox = document.querySelector('.circle-box')
let RotationBox = document.querySelector('.Rotation-box')
let timer = null
let num = 0
let circleIndex
nextPage.onclick = nextBtn
// 下一张事件
function nextBtn() {
    num++
    if (num == ul.children.length-1) {
        ul.style.left = 0
        num = 0
    }
    ul.style.left = ul.offsetLeft - ul.children[0].clientWidth + 'px'
    circleEvent(num)
}
// 上一张事件
prePage.onclick = function () {
    if(num == 0){
        ul.style.left = -ul.clientWidth +ul.children[0].clientWidth + 'px'
        num = ul.children.length-1
    }
    num--
    ul.style.left = ul.offsetLeft + ul.children[0].clientWidth + 'px'
    circleEvent(num)
}
// 循环图片元素自动生成轮播图下面的圆圈
for (let i = 0; i < ul.children.length; i++) {
    // 创建span标签
    let span = document.createElement('span')
    // 给每一span标签自定义属性index
    span.setAttribute('data-index', i)
    // 添加点击事件
    span.addEventListener('click', circleEvent)
    // 插入到父元素circleBox里面
    circleBox.appendChild(span)
}
// 复制第一张照片,即第一个li元素节点
let node = ul.children[0].cloneNode(true)
ul.appendChild(node)
circleBox.children[0].style.background = '#fff'
// 鼠标进入事件
RotationBox.addEventListener('mouseover', function () {
    nextPage.style.display = 'block'
    prePage.style.display = 'block'
    clearInterval(timer)
})
// 鼠标离开事件
RotationBox.addEventListener('mouseleave', function () {
    nextPage.style.display = 'none'
    prePage.style.display = 'none'
    timer = setInterval(function () {
        nextBtn()
    }, 2000)
})
// 下面圆圈点击事件
function circleEvent(e) {
    if(e instanceof Object){
        num = e.target.dataset.index
    }
    ul.style.left = -(num * ul.children[0].clientWidth) + 'px'
    for (let i = 0; i < circleBox.children.length; i++) {
        circleBox.children[i].style.background = ''
    }
    if(num == 4){
        num = 0
    }
    circleBox.children[num].style.background = '#fff'
}
timer = setInterval(function () {
    nextBtn()
}, 2000)

没有动画效果,暂时还不会动画,可能后面会补上

方法二:

1.HTML

<div class="slideshow-container">
  <div class="mySlides fade">
    <div class="numbertext">1 / 3</div>
    <img src="https://c.runoob.com/wp-content/uploads/2017/01/img_mountains_wide.jpg" style="width:100%">
    <div class="text">文本 1</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">2 / 3</div>
    <img src="https://c.runoob.com/wp-content/uploads/2017/01/img_fjords_wide.jpg" style="width:100%">
    <div class="text">文本 2</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">3 / 3</div>
    <img src="https://c.runoob.com/wp-content/uploads/2017/01/img_nature_wide.jpg" style="width:100%">
    <div class="text">文本 3</div>
  </div>

   <a class="prev" onclick="plusSlides(-1)">❮</a>
  <a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>

<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
</div>

 2.css

* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;}
.mySlides {display:none}
/* 幻灯片容器 */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* 下一张 & 上一张 按钮 */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}

/* 定位 "下一张" 按钮靠右 */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* 标题文本 */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* 数字文本 (1/3 等) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* 标记符号 */
.dot {
  cursor:pointer;
  height: 13px;
  width: 13px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  background-color: #717171;
}

/* 淡出动画 */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

3.js

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1} 
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none"; 
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block"; 
  dots[slideIndex-1].className += " active";
}

第二种方法很多代码是写死的,如果想动态渲染,可以像第一那样写,第二种方法也没有动画,不过加了淡入淡出效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值