向下滚动到移动设备

时间:2015-06-03 11:33:05

标签: javascript jquery html css

我几天前开始学习JavaScript,到目前为止我很享受。

今天我已经完成了一项任务,我需要在单击按钮时向元素添加向下滚动功能,但前提是屏幕宽度小于或等于699像素。我已经完成了一个网页,我只需要为它添加JavaScript。

这是我目前的脚本片段:

$(document).ready(function (){
    $("#button").click(function (){
        //$(this).animate(function(){
            $('html, body').animate({
                scrollTop: $("#searchResult").offset().top
            }, 2000);
        //});
    });
});

我需要补充一下:

if (screen.width <= 699)

如何将if语句添加到上面的脚本中?还有一种更简单的方法吗?

非常感谢你的时间,

2 个答案:

答案 0 :(得分:0)

$(document).ready(function (){
var screenWidth = $(window).width();
    $("#button").click(function (){
        if (screenWidth <= 699){
            $('html, body').animate({
                scrollTop: $("#searchResult").offset().top
            }, 2000);
        }
        //other stuff your button does on all views
    });
});

我建议将手机设置为480px,将平板电脑设置为768px。

答案 1 :(得分:0)

使用此

var width = $(window).width();
if(width < 699) {
//your script
}