简体   繁体   English

JS不更新enquire.js断点

[英]JS not updating on enquire.js breakpoints

So I am fairly new to JS/jQuery. 因此,我对JS / jQuery相当陌生。 Recently found enquire.js as I wanted to load different JS styles to affect my collapsing header at different screen widths. 最近发现了enquire.js,因为我想加载不同的JS样式以在不同的屏幕宽度下影响折叠的标题。 I have a scroll event listener, listening for if scroll past 250 execute, else go back to normal. 我有一个滚动事件监听器,监听是否滚动超过250个执行,否则恢复正常。

JS here: JS在这里:

var header, banner, nav, headertxt, pagetop, yPos; var header,banner,nav,headertxt,pagetop,yPos;

    function yScroll() {
        header = document.getElementById("header");
        banner = document.getElementById("banner");
        nav = document.getElementById("nav");
        headertxt = document.getElementById("headertxt");
        pagetop = document.getElementById("pagetop");
        yPos = window.pageYOffset;
        if (yPos > 250) {
            header.style.height = "50px";
            //banner.style.top = "50px";
            //banner.style.height = "800px";
            nav.style.top = "45%";
            headertxt.style.top = "23%";
            pagetop.style.top = "140px";
        } else {
            header.style.height = "100px";
            //banner.style.top = "100px";
            //banner.style.height = "800px";
            nav.style.top = "75%";
            headertxt.style.top = "32%";
            pagetop.style.top = "107px";
        }
    }
    window.addEventListener("scroll", yScroll);

What was happening I was getting the desired effect although an obvious major problem. 尽管发生了明显的重大问题,但正在发生的事情却达到了预期的效果。 The JS was only executing after I scrolled on the page. 我在页面上滚动后才执行JS。 So if I resized nothing would happen. 因此,如果我调整大小,将不会发生任何事情。 I want the styles to change on the fly then listen whether I scroll then use my code above. 我希望样式可以随时更改,然后听是否滚动然后使用上面的代码。 I am sure there is a more efficient way of doing this. 我相信有一种更有效的方法可以做到这一点。 Just with my knowledge and experience I tried this.... 仅凭我的知识和经验,我就尝试了此方法。

enquire.register("screen and (min-width: 600px) and (max-width: 899px)", {
        match : function() {
            var header, banner, nav, headertxt, pagetop, yPos, iwidth;

                function Resize() {
                    header = document.getElementById("header");
                    banner = document.getElementById("banner");
                    nav = document.getElementById("nav");
                    headertxt = document.getElementById("headertxt");
                    pagetop = document.getElementById("pagetop");
                    iwidth = window.innerWidth;
                    if (iwidth < 899) {
                        header.style.height = "75px";
                        nav.style.top = "72%";
                        headertxt.style.top = "32%";
                        headertxt.style.fontSize ="21px";
                        pagetop.style.top = "107px";
                    }
                }
                window.addEventListener("onresize", Resize);

                function yScroll() {
                    header = document.getElementById("header");
                    banner = document.getElementById("banner");
                    nav = document.getElementById("nav");
                    headertxt = document.getElementById("headertxt");
                    pagetop = document.getElementById("pagetop");
                    yPos = window.pageYOffset;
                    if (yPos > 250) {
                        header.style.height = "35px";
                        //banner.style.top = "50px";
                        //banner.style.height = "800px";
                        nav.style.top = "45%";
                        headertxt.style.top = "20%";
                        headertxt.style.fontSize = "17px";
                        pagetop.style.top = "140px";
                    } else {
                        header.style.height = "75px";
                        //banner.style.top = "100px";
                        //banner.style.height = "800px";
                        nav.style.top = "72%";
                        headertxt.style.top = "32%";
                        headertxt.style.fontSize = "21px";
                        pagetop.style.top = "107px";
                    }
                }
                window.addEventListener("scroll", yScroll);

        }
    });


    enquire.register("screen and (min-width: 900px)", {
        match : function() {
            var header, banner, nav, headertxt, pagetop, yPos, iwidth;

                function Resize() {
                    header = document.getElementById("header");
                    banner = document.getElementById("banner");
                    nav = document.getElementById("nav");
                    headertxt = document.getElementById("headertxt");
                    pagetop = document.getElementById("pagetop");
                    iwidth = window.innerWidth;
                    if (iwidth > 899) {
                        header.style.height = "100px";
                        nav.style.top = "75%";
                        headertxt.style.top = "32%";
                        headertxt.style.fontSize ="21px";
                        pagetop.style.top = "107px";
                    }
                }
                window.addEventListener("onresize", Resize);

                function yScroll() {
                    header = document.getElementById("header");
                    banner = document.getElementById("banner");
                    nav = document.getElementById("nav");
                    headertxt = document.getElementById("headertxt");
                    pagetop = document.getElementById("pagetop");
                    yPos = window.pageYOffset;
                    if (yPos > 250) {
                        header.style.height = "50px";
                        //banner.style.top = "50px";
                        //banner.style.height = "800px";
                        nav.style.top = "45%";
                        headertxt.style.top = "23%";
                        headertxt.style.fontSize ="21px";
                        pagetop.style.top = "140px";
                    } else {
                        header.style.height = "100px";
                        //banner.style.top = "100px";
                        //banner.style.height = "800px";
                        nav.style.top = "75%";
                        headertxt.style.top = "32%";
                        headertxt.style.fontSize ="21px";
                        pagetop.style.top = "107px";
                    }

                }
                window.addEventListener("scroll", yScroll);

        }
    });

So as you can see I tried listening for resize and executing the styles when the innerwidth was < 899 for example. 如您所见,例如,当innerwidth小于899时,我尝试侦听调整大小并执行样式。 The resize function does not seem to be working at all. 调整大小功能似乎根本不起作用。 My scroll function is still working as normal with nothing happening when I resize the window. 调整窗口大小时,我的滚动功能仍然可以正常工作。 Now I will be wanting to change and add to the style.height .top etc in the future as this project still in fairly early stages. 现在,我将要在将来更改并添加到style.height .top等中,因为该项目仍处于早期阶段。

I also thought of just coding the styles in my CSS breakpoints but the scroll listener overrides the CSS? 我还想到了只是在CSS断点中编码样式,但是滚动侦听器会覆盖CSS? Or just take the else out of my scroll function so the CSS will be the else part instead of the JS? 或者只是从我的滚动功能中删除else,以便CSS将成为else部分而不是JS? I am really just thinking out loud here. 我真的只是在这里大声思考。

Again still learning. 再次还在学习。 Also just to mention I have used JS on purpose as I am building my portfolio and want to show JS skills as none of my other projects have much JS that I can show. 还要提一下,我在构建自己的产品组合时就故意使用了JS,并希望展示JS技能,因为我的其他项目都没有我可以展示的JS。 Thanks in advance for any guidance. 在此先感谢您的指导。 SOLVED 解决了

The event string is 'resize' , not 'onresize' . 事件字符串是'resize' ,而不是'onresize' :P :P

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

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