如何停止幻灯片显示在鼠标上(幻灯片显示应该像往常一样开始后鼠标退出)在java脚本

How stop slide show on mouse over (slide show should start as usual after mouse out) in java script

本文关键字:显示 鼠标 幻灯片 开始 退出 java 脚本 常一样 何停止      更新时间:2023-09-26

嗨,我花了10多个小时来停止鼠标上方的幻灯片播放,并开始鼠标移出。我在堆栈溢出中看到了一些建议,我没有得到任何解决方案(可能这是我的错误,根据我的代码来理解其他人的代码。因此,我给我的代码)。

代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/>
<title>Simple jQuery Slideshow from JonRaasch.com</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">
/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/
function slideSwitch() {
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
    // uncomment the 3 lines below to pull the images in random order
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
$(function() {
    setInterval( "slideSwitch()", 1500 );
});
$("#slideshow")(function() { $(this).slides("stop"); });
//playing all slides
$("#slideshow")(function() { $(this).slides("play"); });
</script>
<style type="text/css">
/*** set the width and height to match your images **/
#slideshow {
    position:relative;
    height:350px;
    width: 40%;
}
#slideshow IMG {
    position:absolute;
    top:0;
    left:0;
    z-index:8;
    opacity:0.0;
}
#slideshow IMG.active {
    z-index:10;
    opacity:1.0;
}
#slideshow IMG.last-active {
    z-index:9;
}
</style>
</head>
<body style="font-family: Arial, Sans-serif, sans;">

<!-- this will work with any number of images -->
<!-- set the active class on whichever image you want to show up as the default 
(otherwise this will be the last image) -->
<div id="slideshow">
    <img src="image1.jpg" alt="Slideshow Image 1" class="active" />
    <img src="image2.jpg" alt="Slideshow Image 2" />
    <img src="image3.jpg" alt="Slideshow Image 3" />
    <img src="image4.jpg" alt="Slideshow Image 4" />
</div>



</body>
</html> 

这个jsfiddle应该是你正在寻找的一个例子。

你的代码中有一些奇怪的部分,比如$("#slideshow")(function() { $(this).slides("play"); });,没有意义。我删除了我的例子。

我的例子包括在Johan Van den Rym的回答中使用的悬停绑定。

基本上,我添加了一个布尔值来跟踪幻灯片是否被悬停:

var isStopped = false; function slideSwitch() { if (!isStopped) { ...

最后我添加了悬停状态

$("#slideshow").hover(function () { isStopped = true; }, function () { isStopped = false; });

$("#slideshow").hover(
  function() {
    $(this).slides("stop");
  }, 
  function() {
    $(this).slides("play");
});