在移动设备上向下滚动到搜索结果页面

Scroll-down to the search result page in mobile

本文关键字:滚动 搜索结果 移动      更新时间:2023-09-26

我几天前开始学习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语句添加到上面的脚本中?还有,有没有更简单的方法?

非常感谢您的宝贵时间,

$(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。

使用

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