简体   繁体   English

JavaScript最大宽度removeAttribute

[英]JavaScript max-width removeAttribute

I have a problem with this code. 我对此代码有疑问。

This code is checking the resolution of mobile device and open a web page with same resolution. 此代码用于检查移动设备的分辨率并打开具有相同分辨率的网页。 But When I open a page from the mobile device with display width less than 480px and I scroll, the page is not viewed correctly. 但是,当我从移动设备打开显示宽度小于480px的页面并滚动时,该页面无法正确查看。 I want to drop this code for devices with display width less than 480px. 我想为显示宽度小于480px的设备删除此代码。

Can you help me? 你能帮助我吗?

JavaScript 的JavaScript

function init() {
    window.addEventListener('scroll', function(e){
        var distanceY = window.pageYOffset || document.documentElement.scrollTop, 
            shrinkOn = 200,
            header = document.querySelector("nav");
        if (distanceY > shrinkOn) {
            header.setAttribute("id","smaller");
        } else {
            header.removeAttribute("id","smaller");
        }
    });
}

I dont want: 480px width id smaller to be added. 我不想:480px宽度id smaller要添加。

If I understand you correctly, you want this line of code: header.setAttribute("id","smaller"); 如果我理解正确,则需要以下代码行:header.setAttribute(“ id”,“ smaller”); to be only executed if the width is greater then 480px. 仅在宽度大于480px时执行。 If this is the case, try the following: 如果是这种情况,请尝试以下操作:

var width = document.body.clientWidth;

function init() {
    window.addEventListener('scroll', function(e){
        var distanceY = window.pageYOffset || document.documentElement.scrollTop, 
            shrinkOn = 200,
            header = document.querySelector("nav");
        if (distanceY > shrinkOn) {
            if (width > 480) {
            header.setAttribute("id","smaller");
            }           
        } else {
            header.removeAttribute("id","smaller");
        }
    });
}

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

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