求一个最简单或者说,代码最少的jquery slide代码?

作者站长头像
站长
· 阅读数 15

功能有向前一个,向后一个,图片切换效果常规就行,带小点导航,

在网上找的代码,如下,目前无法自动开启,自动 开启当鼠标进入图区不暂停,退出不继续,求指教,小白

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.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);
}

/* Caption text */
.text {
    position: absolute;
    bottom: -43px;
    width: 100%;
    height: 43px;
    line-height: 43px;
    white-space: nowrap;
    text-align: center;
    background-color: #555
}
.text a{    
    display: block;
    color: #fff;
    font-size: 18px;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot_old {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}



.dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    margin-right: 5px;
    border-radius: 3px;
    box-sizing: border-box;
    background-color: #fff;
    transition: width .3s;
}
.active, .dot:hover {
    width: 10px;
    opacity: 1;
    background-color: #206cfe;
}

/* Fading animation */
.fade {
  animation-name: fade;
  animation-duration: 1.5s;
}

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

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .prev, .next,.text {font-size: 11px}
}

.slide_dot{
    position: absolute;
    left: 0;
    bottom: 10px;
    right: 0;
    z-index: 3;
    font-size: 0;
    text-align: center;
    margin-top: 6px;
}
</style>
</head>
<body>

<div class="slideshow-container" id="ss">
    <div class="mySlides fade">
    <div class="numbertext">1 / 3</div>
    <img src="img/img_nature_wide.jpeg" style="width:100%">
    <div class="text"><a href="#">那到底值不值 点评奔驰GLB 180</a></div>
    </div>

    <div class="mySlides fade">
    <div class="numbertext">2 / 3</div>
    <img src="img/img_snow_wide.jpeg" style="width:100%">
    <div class="text"><a href="#">那到底值不值 点评奔驰GLB 180</a></div>
    </div>

    <div class="mySlides fade">
    <div class="numbertext">3 / 3</div>
    <img src="img/img_mountains_wide.jpeg" style="width:100%">
    <div class="text"><a href="#">那到底值不值 点评奔驰GLB 180</a></div>
    </div>

    <a class="prev" onclick="plusSlides(-1)">❮</a>
    <a class="next" onclick="plusSlides(1)">❯</a>
    <div class="slide_dot" 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>
</div>



<script src="https://www.w3school.com.cn/jquery/jquery-1.11.1.min.js"></script>
<script>
let slideIndex = 0;
showSlides();

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

function currentSlide(n) {
  showSlides(slideIndex = n);
}
let slide_s=MysetTimeout(showSlides, 2000);
function showSlides(n) {
  let i;
  let slides = document.getElementsByClassName("mySlides");
  let dots = document.getElementsByClassName("dot");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  slideIndex++;
  if (slideIndex > slides.length) {slideIndex = 1}    
  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";
  slide_s; // Change image every 2 seconds
}
function MysetTimeout(callback, delay) {
    var setTimeoutId, start, remaining = delay;

    this.pause = function () {
        window.clearTimeout(setTimeoutId);
        remaining -= new Date() - start;
    };

    this.play= function () {
        start = new Date();
        window.clearTimeout(setTimeoutId);
        setTimeoutId= window.setTimeout(callback, remaining);
    };

    this.play();
}
$("#s").mouseenter(function(){
    console.log("pause")
});
$("#s").mouseleave(function(){
    console.log("play")
});

</script>

</body>
</html> 
回复
1个回答
avatar
test
2024-06-29

给你改了一下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.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);
}

/* Caption text */
.text {
    position: absolute;
    bottom: -43px;
    width: 100%;
    height: 43px;
    line-height: 43px;
    white-space: nowrap;
    text-align: center;
    background-color: #555
}
.text a{    
    display: block;
    color: #fff;
    font-size: 18px;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot_old {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}



.dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    margin-right: 5px;
    border-radius: 3px;
    box-sizing: border-box;
    background-color: #fff;
    transition: width .3s;
}
.active, .dot:hover {
    width: 10px;
    opacity: 1;
    background-color: #206cfe;
}

/* Fading animation */
.fade {
  animation-name: fade;
  animation-duration: 1.5s;
}

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

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .prev, .next,.text {font-size: 11px}
}

.slide_dot{
    position: absolute;
    left: 0;
    bottom: 10px;
    right: 0;
    z-index: 3;
    font-size: 0;
    text-align: center;
    margin-top: 6px;
}
</style>
</head>
<body>

<div class="slideshow-container" id="ss">
    <div class="mySlides fade">
        <div class="numbertext">1 / 3</div>
        <img src="img/img_mountains_wide.jpeg" style="width:100%">
        <div class="text"><a href="#">那到底值不值 点评奔驰GLB 180</a></div>
    </div>

    <div class="mySlides fade">
        <div class="numbertext">2 / 3</div>
        <img src="img/img_nature_wide.jpeg" style="width:100%">
        <div class="text"><a href="#">那到底值不值 点评奔驰GLB 180</a></div>
    </div>

    <div class="mySlides fade">
        <div class="numbertext">3 / 3</div>
        <img src="img/img_snow_wide.jpeg" style="width:100%">
        <div class="text"><a href="#">那到底值不值 点评奔驰GLB 180</a></div>
    </div>

    <a class="prev" onclick="plusSlides(-1)">❮</a>
    <a class="next" onclick="plusSlides(1)">❯</a>
    <div class="slide_dot" 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>
</div>


<script src="https://www.w3school.com.cn/jquery/jquery-1.11.1.min.js"></script>
<script>
let slideIndex = 0;

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

function currentSlide(n) {
  showSlides(slideIndex = n);
}
let slide_s, setTimeoutId
function showSlides() {
  let i;
  let slides = document.getElementsByClassName("mySlides");
  let dots = document.getElementsByClassName("dot");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  slideIndex++;
  if (slideIndex > slides.length) {slideIndex = 1}
  if(slideIndex < 1){slideIndex = slides.length}
  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";
  slide_s = MysetTimeout(showSlides, 2000); // Change image every 2 seconds
}
function MysetTimeout(callback, delay) {
    var obj = {}
    obj.pause = function () {
        window.clearTimeout(setTimeoutId);
    };

    obj.play= function () {
        window.clearTimeout(setTimeoutId);
        setTimeoutId= window.setTimeout(callback, delay);
    };

    obj.play();
    
    return obj
}

$("#ss").mouseenter(function(){
    console.log("pause")
    slide_s.pause()
}).mouseleave(function(){
    console.log("play")
    slide_s.play()
});

showSlides();
</script>

</body>
</html> 
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容