简体   繁体   English

如何设置jQuery断点?

[英]How to set jQuery breakpoints?

Because this is my first post, so in the beginning welcome everyone. 因为这是我的第一篇文章,所以一开始欢迎大家。

Since I'm just starting in JS and jQuery, can someone suggest how to deal with jQuery breakpoints? 由于我刚开始使用JS和jQuery,有人可以建议如何处理jQuery断点吗?

On the website, I have a menu at the top in position: fixed and height: 200px . 在网站上,我的菜单位于顶部position: fixedheight: 200px In the menu linked to scroll to a specific section using jQuery. 在链接的菜单中,使用jQuery滚动到特定部分。 The selected section goes to the top of the page. 所选部分转到页面顶部。

I have created breakpoints in CSS and eg below 1024px , the menu is already 100px hight. 我已经在CSS中创建了断点,例如在1024px以下,菜单已经是100px高。 Do you have an idea on how to convert a rule in jQuery, so that below 1024px scrolls to top - 100 ? 您是否有关于如何在jQuery中转换规则的想法,以便在1024px以下滚动到top - 100

  <nav>
        <a class="region" href="#">Okolica</a>

    </nav>

 <section class="s1"> <section>
nav {
    height: 200px;

}

@media (max-width: 1024px) {

    nav {
        height: 100px;
    }
}
$('.region').on('click', function () {
    $('body, html').animate({
        scrollTop: $('.s1').offset().top - 200
    }, 2000)
})

I tried code below but it doesn't work. 我在下面尝试了代码,但是没有用。

document.addEventListener('DOMContentLoaded', init);
function init(){
let query = window.matchMedia("(max-width: 1024px)");
if(query.matches){
    $('.region').on('click', function () {
        $('body, html').animate({
            scrollTop: $('.s1').offset().top - 100
        }, 2000)
    })
}}

Any ideas? 有任何想法吗?

you can extract the window size with vanilla javascript 您可以使用香草javascript提取窗口大小

https://developer.mozilla.org/en-US/docs/Web/API/window/innerWidth https://developer.mozilla.org/zh-CN/docs/Web/API/window/innerWidth

window.innerWidth

In jQuery you can write a breakpoint like this: 在jQuery中,您可以编写这样的断点:

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

    //YOUR FUNCTION FOR EXAMPLE
     $('.region').on('click', function () {
        $('body, html').animate({
            scrollTop: $('.s1').offset().top - 100
        }, 2000)
    })

  }

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

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