简体   繁体   English

如何在移动设备上禁用滚动以使此脚本$ align?

[英]How do I disable the scroll to $align of this script on mobile?

I don't understand jQuery enough to either search for the solution, or try to implement one on my own. 我对jQuery的了解不够,无法搜索解决方案,或者尝试自行实现一个解决方案。 On desktop, I use a slideToggle to show/hide content based on which button is clicked. 在桌面上,我使用slideToggle基于单击哪个按钮来显示/隐藏内容。 If the content window is 'shown' I scroll the screen to the top of the '#section' holding the content. 如果“显示”了内容窗口,则将屏幕滚动到保存内容的“ #section”的顶部。 I want to disable this on Mobile, becasue the content stacks into a single column on mobile and if the user clicks on the bottom of the stack, it scrolls him back to the top of the section. 我想在移动设备上禁用此功能,因为内容堆栈会在移动设备上显示为一列,并且如果用户单击堆栈底部,则会将其滚动回到该部分的顶部。

This is the script: 这是脚本:

var $align = $("#features");
function slideonlyone(thechosenone) {
    $('.feature-box').each(function(index) {
        if ($(this).attr("id") == thechosenone) {
            $(this).slideToggle(400);

            $("body").animate({
                scrollTop: $align.offset().top
            },{
                duration: 1200,
                queue: false
            })
        }
        else {
            $(this).slideUp(400);
        }
    });
}

The site can be viewed here: 该站点可以在这里查看:

www.newmarketsolar.ca www.newmarketsolar.ca

If you enable the inspector and turn on mobile emulation you can see what I mean by clicking on 'learn more' button on the last icon 'interactive' - it will scroll you up - I need to turn this off for devices only up to 768px. 如果启用检查器并打开移动仿真,则可以通过单击最后一个图标“交互式”上的“了解更多”按钮来了解我的意思-它会向上滚动-我需要将其关闭,仅适用于768像素以下的设备。

Any help would be marvelous. 任何帮助都会很棒。

So what you want to do is check if the screen width is smaller than 768px first, you can do this with: 因此,您要做的就是先检查屏幕宽度是否小于768像素,您可以使用以下方法进行操作:

slideonlyone(thechosenone){
    if($( window ).width() > 767){
       // your function body
    }
}

This checks if your window is bigger than 767px and otherwise doesn't execute. 这将检查您的窗口是否大于767px,否则不执行。

edit: I didn't look at the site provided, but essentially this is what you need and can place as a condition around anything that should only happen on screens bigger than 767px 编辑:我没有看过提供的站点,但是本质上这是您所需要的,并且可以作为只能在大于767px的屏幕上发生的任何事情的条件

The below page has excellent article on Media query listeners in javascript, http://www.javascriptkit.com/dhtmltutors/cssmediaqueries4.shtml 下面的页面上有一篇关于javascript中媒体查询监听器的出色文章, 网址为http://www.javascriptkit.com/dhtmltutors/cssmediaqueries4.shtml

The below code should take care of your requirement, 以下代码可以满足您的要求,

var mql = window.matchMedia("screen and (min-width: 768px)");

if (mql.matches){ // if media query matches

var $align = $("#features");
function slideonlyone(thechosenone) {
    $('.feature-box').each(function(index) {
        if ($(this).attr("id") == thechosenone) {
            $(this).slideToggle(400);

            $("body").animate({
                scrollTop: $align.offset().top
            },{
                duration: 1200,
                queue: false
            })
        }
        else {
            $(this).slideUp(400);
        }
    });
}

}

The above code runs only once and should work fine on devices or when the page is loaded keeping the browser width less than 768px. 上面的代码只能运行一次,并且应该在设备上正常运行,或者在加载页面时保持浏览器宽度小于768px。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM