【问题标题】:Flexslider body scroll only for current slide (V2)Flexslider body 滚动仅适用于当前幻灯片 (V2)
【发布时间】:2013-10-18 23:41:42
【问题描述】:

我有一个 Flexslider,其中包含一个项目的所有内容。在这种情况下,每个“页面”或幻灯片都有不同的内容,例如更新、项目、关于和联系方式。

页面有不同的长度,有些需要滚动条,有些则不需要。在不需要滚动的页面上,内容下方有很大的空白区域。该空间被其他页面上的内容占用。

我正在使用 jQuery 来查找 li.flex-active-slide 和窗口的高度,这很好用。当li.flex-active-slide 类在单击导航时发生更改时,我无法弄清楚如何告诉 jQuery。

这是我的 Flexslider 代码:

$(document).ready(function() {
    //set some variables for calculating the hash
    var index = 0, hash = window.location.hash;
    //via malsup (Cycle plugin), calculates the hash value
    if (hash) {
        index = /\d+/.exec(hash)[0];
        index = (parseInt(index) || 1) - 1;
    }
    $(window).trigger('resize');
    $('#mainflexslider').flexslider({
        animation: "fade",
        slideDirection: "horizontal",
        keyboardNav: false,
        slideshow: false,
        animationSpeed: 500,
        controlsContainer: ".flex-container",
        manualControls: ".flex-control-nav li",
        startAt: index,
        after:function(slider){
            window.location.hash = slider.currentSlide+1;
        }
    });
});

(旁注:点击导航时,URL 正在更新。问题和相应答案的信用是here。)

以及寻找高度:

$(window).load(function() {
    var slideHeight = $("li.flex-active-slide").height();
    var windowHeight = $(window).height();

    if (slideHeight > windowHeight) {
        $('html, body').css('overflowY', 'auto');
    }
    else {
        $('html, body').css('overflowY', 'hidden');
    }
});

我想知道是否有办法将这两个代码结合起来?

如果没有,当 Flexslider 附加 li.flex-active-slide 类时,我如何告诉 jQuery?

【问题讨论】:

  • 你可以把代码在after回调里面找高度

标签: jquery scroll flexslider


【解决方案1】:

你可以试试这个

$(window).load(function() {
var index = 0, hash = window.location.hash;
//via malsup (Cycle plugin), calculates the hash value
if (hash) {
    index = /\d+/.exec(hash)[0];
    index = (parseInt(index) || 1) - 1;
}
$(window).trigger('resize');
$('.flexslider').flexslider({
    animation: "fade",
    slideDirection: "horizontal",
    keyboardNav: false,
    slideshow: false,
    animationSpeed: 500,
    controlsContainer: ".flex-container",
    manualControls: ".flex-control-nav li",
    startAt: index,
    after:function(slider){
        window.location.hash = slider.currentSlide+1;
        sliderheight();
    },
    start:function(slider){
        sliderheight();
    }
});
function sliderheight(){
    var slideHeight = $("li.flex-active-slide").height();
        var windowHeight = $(window).height();
        console.log(slideHeight+" > "+windowHeight);
        if (slideHeight > windowHeight) {
            $('html, body').css('overflow-y', 'auto');
        }
        else {
            $('html, body').css('overflow-y', 'hidden');
        }
}
});    

用overflow-y的所有代码创建一个函数并从回调中调用它,它可以在之前,之后或开始

【讨论】:

  • 完美!需要进行一些细微的修改,但效果很好!非常感谢你的帮助! :)
猜你喜欢
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多