如何在Java脚本中停止幻灯片放映鼠标(幻灯片播放应该像往常一样在鼠标输出后启动)

时间:2014-03-24 19:55:25

标签: javascript jquery html css

嗨,我花了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> 

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

This 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; });