取消鼠标悬停时的Jquery滑动条事件

Cancel Jquery Slider event on mousehover

本文关键字:事件 Jquery 鼠标 悬停 取消      更新时间:2023-09-26

我编写了以下代码来尝试取消鼠标悬停时的事件。目标是在幻灯片当前处于悬停状态时防止发生滑动事件。如果当前幻灯片处于悬停状态,是否可以取消幻灯片事件?下面是我的代码:

Jquery

<script type="text/javascript">
$(document).ready(function(){
  var currentPosition = 0;
  var slideWidth = 560;
  var slides = $('.slide');
  var numberOfSlides = slides.length;
  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');
  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
      'float' : 'left',
      'width' : slideWidth
    });
  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);
  // Insert controls in the DOM
  $('#slideshow')
    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    .append('<span class="control" id="rightControl">Clicking moves right</span>');
  // Hide left arrow control on first load
  manageControls(currentPosition);
  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
    currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
    // Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });

     // manageControls: Hides and Shows controls depending on currentPosition
      function manageControls(position){
                if(position >= 4)
                {
                    position=0;
                    currentPosition=0;
                }
      } 
      function Aplay(){
        // Determine new position
        currentPosition =  currentPosition+1 ;
        // Hide / show controls
        manageControls(currentPosition);
        // Move slideInner using margin-left
        $('#slideInner').animate({
          'marginLeft' : slideWidth*(-currentPosition)
        });
          setTimeout(function(){Aplay();},5000);
      }
      setTimeout(Aplay(),50000);
});
</script>

<div id="slideshow">
    <div id="slidesContainer">
      <div class="slide">
        <h2>Web Development Tutorial</h2>
        <p><a href="#"><img src="img/img_slide_01.jpg" alt="An image that says Install X A M P P for wordpress." width="215" height="145" /></a>If you're into developing web apps, you should check out the tutorial called "<a href="#">Using XAMPP for Local WordPress Theme Development</a>"</p>
      </div>
      <div class="slide">
        <h2>Grunge Brushes, Anyone?</h2>
        <p><a href="#"><img src="img/img_slide_02.jpg" width="215" height="145" alt="A thumbnail image that says S R grunge photoshop brushes 6 high resolution grunge brushes by six revisions." /></a>In this layout, I used <a href="#">SR Grunge</a>,</p>
        <p> 
      </div>
      <div class="slide">
        <h2>How About Some Awesome Grunge Textures?</h2>
        <p><a href="#"><img src="img/img_slide_03.jpg" width="215" height="145" alt="A thumbnail image that says grunge extreme 15 free high resolution grunge textures six revisions." /></a>The texture used in this web page is from the Grunge Extreme Textures freebie set by JC Parmley released here on Six Revisions.</p>
        <p>You can head over to the <a href="#">Grunge Extreme</a> page to download the texture set or check out Six Revisions' <a href="#">freebie section</a> for even more goodies!</p>
      </div>
      <div class="slide">
        <h2>'Tis the End, My Friend.</h2>
        <p><a href="#"><img src="img/img_slide_04.jpg" width="215" height="145" alt="Thumbnail image that says sleek button using photoshop that links to a Photoshop tutorial." /></a></p>
        <p> web interface.</p>
      </div>
    </div>
  </div>
  <!-- Slideshow HTML -->
</div>

现场演示

用下面的代码替换Javascript的底部部分:

 var timeout = null;
      function Aplay(){
        // Determine new position
        currentPosition =  currentPosition+1 ;
        // Hide / show controls
        manageControls(currentPosition);
        // Move slideInner using margin-left
        $('#slideInner').animate({
          'marginLeft' : slideWidth*(-currentPosition)
        });
        timeout = setTimeout(function(){Aplay();},5000);
      }
      timeout = setTimeout(function(){Aplay();},5000);
    $('#slideshow').hover(function(){
        clearTimeout(timeout);
    },function(){
       timeout = setTimeout(function(){Aplay();},5000);
    });

Exaplanation:

创建一个名为timeout的变量,该变量将包含setTimeout事件
$('slideshow').hover(...)将在鼠标经过该事件时停止该事件,然后在鼠标退出幻灯片时将其重新分配给timeout

编辑

OP对下面问题的解答:

添加到html:

<span id="numbering">1</span>

在JS中添加以下函数:

function updateNumber(){
     $('#numbering').text((+$('#numbering').text())%4 + 1);
}

然后在涉及图像滑动的函数中调用它,所以当文本被点击和自动滑动时。

现场演示

.stop()将停止所有动画,因此尝试在滑动条元素中添加鼠标悬停

<div id="slide1" class="slide" onmouseover='$("#" + this.id).stop();'></div>

或者你可以绑定监听器

$('.slide').mouseover(function(){ $("#" + this.id).stop(); });

你应该使用

.hover( handlerIn, handlerOut )
jQuery .hover() API将帮助你实现你想要的。