在窗口宽度低于468px时调用jquery函数

时间:2014-06-18 07:00:37

标签: jquery

滚动时,我有一个jquery代码来修复页面底部的div。

var ad = jQuery('.top_leaderboard_container');
var min_scroll = 25; // Set your minimum scroll amount here
jQuery(window).scroll(
    function() {
        var t = jQuery(window).scrollTop();
        if (t > min_scroll) {
            // define your scroll CSS here

            ad.css({position: "fixed" , bottom:"0"});
        } else {
            // define your non-scrolled CSS here

            ad.css({position: "relative"});
        }
    }
);

我希望只有当网站的宽度低于468px时才会发生此功能。 我必须添加什么jquery代码才能实现这个目标?

3 个答案:

答案 0 :(得分:1)

// call your code on page load
jQuery(document).ready(function () {
    widthSmallerThan(468);
});

// call it every time the window size changes and is smaller than...
jQuery(window).resize(function () {
    widthSmallerThan(468);
});

var widthSmallerThan = function(width) {
    if (jQuery(window).width() < width) {
        // your code
    }
};

答案 1 :(得分:0)

if ($(window).width() < 468) {

    // your logic here

}

这样就可以了解

答案 2 :(得分:0)

您可以使用CSS3中提供的媒体查询。

@media (max-width: 468px) {
  .top_leaderboard_container{position: "relative";}
}

@media (min-width: 469px) {
   .top_leaderboard_container{position: "fixed"; bottom:"0";}
}

更多信息请点击here