Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 始终将滑动滑梯滚动到顶部_Javascript_Html_Css_Swiper - Fatal编程技术网

Javascript 始终将滑动滑梯滚动到顶部

Javascript 始终将滑动滑梯滚动到顶部,javascript,html,css,swiper,Javascript,Html,Css,Swiper,我正在使用swiper.js。 我希望总是让新的/活动的幻灯片滚动到顶部,因此当新幻灯片进入视口时,内容必须滚动到顶部 我这里有一把小提琴: 我原以为这个脚本可以满足我的需要,但它似乎不起作用: swiper.on('slideChangeEnd', function(s) { s.slides.each(function() { if ($(this).index() !== s.activeIndex) $(this)[0].scrollTop = 0 }); });

我正在使用swiper.js。 我希望总是让新的/活动的幻灯片滚动到顶部,因此当新幻灯片进入视口时,内容必须滚动到顶部

我这里有一把小提琴:

我原以为这个脚本可以满足我的需要,但它似乎不起作用:

swiper.on('slideChangeEnd', function(s) {
    s.slides.each(function() {
    if ($(this).index() !== s.activeIndex) $(this)[0].scrollTop = 0
   });
});

谢谢

除非您限制容器/幻灯片的高度-导致溢出-可滚动内容实际上不会在每张幻灯片中。 在这种情况下,您应该关注swiper容器或其他父元素的
scrollTop
属性

无论您滚动到什么位置,如果将事件添加到初始化配置中,它都会工作得更好。要使用JSFIDLE中的代码,请执行以下操作:

var swiper = new Swiper('.swiper-container', {
  pagination: '.swiper-pagination',
  paginationClickable: true,
  nextButton: '.swiper-button-next',
  prevButton: '.swiper-button-prev',
  spaceBetween: 30,
  hashnav: true,
  autoHeight: true,

  // attach callback here
  onSlideChangeEnd: function (swiperObj) {
    $('.swiper-container').css('scrollTop', '0');
  }
});

嗯,上面的答案对我不起作用。将此答案添加到不同的场景中

在我的例子中,我在同一个页面上有多个具有不同包装器ID的swiper,我正在根据其ID初始化swiper

由于有多个swiper实例,我无法找到滑块的正确位置,所以我不得不这样做

    var SwiperItems= [];
    $('.swiper-items-wrapper').each(function(i) {
        var this_ID = $(this).attr('id');
        SwiperItems[i] = new Swiper( '#' + this_ID + ' .swiper-contrainer', {
            loop: true,
            navigation: {
                nextEl: '#' + this_ID + ' .swiper-button-next-outsite',
                prevEl: '#' + this_ID + ' .swiper-button-prev-outsite',
            },
            autoHeight: true,
        });
        SwiperItems[i].on('slideChange', function (swiperObj) {
            $('html, body').animate({
                scrollTop: jQuery(SwiperItems[i].$el).offset().top
            }, 1000);
        );
    });

谢谢你。很明显,当你读到它的时候,我已经思考了30分钟了