简体   繁体   English

JS滑块在褪色时跳来跳去

[英]JS slider jumping around when fading

I have an issue with my js slider where by the behaviour is odd. 我的js滑块有个问题,按行为来说是奇怪的。 I am calling Jquery at the top in the header. 我在标题顶部调用Jquery。 the JS script is at the bottom. JS脚本在底部。

The issue when the page loads it shows all three of the slides and quickly shows the first one and hides the other two. 页面加载时出现的问题显示了所有三张幻灯片,并迅速显示了第一张幻灯片并隐藏了另外两张幻灯片。 When they fad in and out to the next slides etc the content jumps down. 当他们淡入和淡出到下一张幻灯片等内容时,内容会跳下来。

You can see the behaviour here: Demo 您可以在此处查看行为: 演示

And here is the JS 这是JS

// settings
var $slider = $('.slider'); // class or id of carousel slider
var $slide = 'li'; // could also use 'img' if you're not using a ul
var $transition_time = 1000; // 1 second
var $time_between_slides = 3000; // 4 seconds

function slides(){
  return $slider.find($slide);
}

slides().fadeOut();

// set active classes
slides().first().addClass('active');
slides().first().fadeIn($transition_time);

// auto scroll 
$interval = setInterval(
    function(){
      var $i = $slider.find($slide + '.active').index();

      slides().eq($i).removeClass('active');
      slides().eq($i).fadeOut($transition_time);

      if (slides().length == $i + 1) $i = -1; // loop to start

      slides().eq($i + 1).fadeIn($transition_time);
      slides().eq($i + 1).addClass('active');
    }
    , $transition_time +  $time_between_slides 
);

Just put position:absolute on the ul lì . 只要把position:absoluteul lì

ul li {
  position:absolute
}

JSFIDDLE: http://jsfiddle.net/9L55zh9o/2/ . JSFIDDLE: http : //jsfiddle.net/9L55zh9o/2/

You had it position:static , which keeps it in the document flow and pushes other elements away https://developer.mozilla.org/en-US/docs/Web/CSS/position#Values . 您拥有了position:static ,它将其保留在文档流中 ,并将其他元素推开https://developer.mozilla.org/en-US/docs/Web/CSS/position#Values

Call the fadeIn function after the fadeOut function is complete. 当fadeOut功能完成后,调用fadeIn功能。

http://jsfiddle.net/9L55zh9o/1 http://jsfiddle.net/9L55zh9o/1

function () {
    var $i = $slider.find($slide + '.active').index();

    slides().eq($i).removeClass('active');
    slides().eq($i).fadeOut($transition_time, function(){
        if (slides().length == $i + 1) $i = -1; // loop to start

        slides().eq($i + 1).fadeIn($transition_time);
        slides().eq($i + 1).addClass('active');
    });
}, $transition_time + $time_between_slides);

its because your transition in and out happen simultaneously - so the in fade creates an element below the out element. 这是因为您的移入和移出是同时发生的-因此入淡入会在out元素下方创建一个元素。 try something like this: 尝试这样的事情:

// settings
var $slider = $('.slider'); // class or id of carousel slider
var $slide = 'li'; // could also use 'img' if you're not using a ul
var $transition_time = 1000; // 1 second
var $time_between_slides = 3000; // 4 seconds

function slides() {
  return $slider.find($slide);
}

slides().fadeOut();

// set active classes
slides().first().addClass('active');
slides().first().fadeIn($transition_time);

// auto scroll 
$interval = setInterval(

function () {
 var $i = $slider.find($slide + '.active').index();

 slides().eq($i).removeClass('active');
 slides().eq($i).fadeOut($transition_time/2, function(){
     if (slides().length == $i + 1) $i = -1; // loop to start

     slides().eq($i + 1).fadeIn($transition_time/2);
     slides().eq($i + 1).addClass('active');
 });


}, $transition_time + $time_between_slides);

fiddle 小提琴

Here an updated jsfiddle : http://jsfiddle.net/9L55zh9o/5/ 这里是更新的jsfiddlehttp : //jsfiddle.net/9L55zh9o/5/

The fadeout takes time, so you have to defer the fadein in a callback function: fadeout需要时间,因此您必须在回调函数中延迟fadein

slides().eq($i).fadeOut($transition_time, function() {
    if (slides().length == $i + 1) $i = -1; // loop to start
    slides().eq($i + 1).fadeIn($transition_time);
    slides().eq($i + 1).addClass('active');  
});

You can also hide all slides at startup with slides().hide(); 您还可以在启动时使用slides().hide();隐藏所有幻灯片slides().hide(); .

Working jsfiddle here http://jsfiddle.net/bevanr01/705u9w1a/ 在这里工作jsfiddle http://jsfiddle.net/bevanr01/705u9w1a/

// settings
var $slider = $('.slider'); // class or id of carousel slider
var $slide = 'li'; // could also use 'img' if you're not using a ul
var $transition_time = 1000; // 1 second
var $time_between_slides = 3000; // 4 seconds

function slides() {
    return $slider.find($slide);
}

slides().hide();

// set active classes
slides().first().addClass('active');
slides().first().fadeIn($transition_time);

// auto scroll 
$interval = setInterval(

function () {
    var $i = $slider.find($slide + '.active').index();

    slides().eq($i).removeClass('active');
    slides().eq($i).fadeOut($transition_time,
                        function() {
    if (slides().length == $i + 1) $i = -1; // loop to start

    slides().eq($i + 1).fadeIn($transition_time);
    slides().eq($i + 1).addClass('active');                                
  });


}, $transition_time + $time_between_slides);

Your transitions are happening simultaneously so it isn't transitioning smoothly. 您的过渡同时发生,因此过渡不顺利。 You should also hide the elements initially instead of fading out to prevent the initial behavior. 您还应该最初隐藏元素,而不是淡出以防止出现初始行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM