取消对mousehover jQuery的滑块事件(Cancel Jquery Slider eve

2019-10-20 09:36发布

我写了下面的代码,以试图在mousehover取消的事件。 我们的目标是,如果从幻灯片目前正徘徊在发生保持幻灯片事件。 是否有可能取消幻灯片事件,如果当前幻灯片正在徘徊? 这里是我的代码:

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>

HTML

<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>

Answer 1:

现场演示

替换与此代码的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);
}

然后调用它在涉及与图像的滑动功能,所以当文字和点击时自动滑动。

现场演示



Answer 2:

.stop()将停止所有动画,所以尽量将鼠标悬停到您的移动元件

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

或者你可以绑定监听器

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


Answer 3:

你应该使用

.hover( handlerIn, handlerOut )

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



文章来源: Cancel Jquery Slider event on mousehover