jQuery: vw instead of px



知道如何将 100px 转换为 100vw 吗?

$(document).ready(function(){
$(window).scroll(function() { 
if ($(document).scrollTop() > 100) { 
$(".header").css("color", "#fff"); 
} else {
$(".header").css("color", "#000"); 
}
});
});

您可以使用以下代码行获取视口宽度:

const width = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);

然后在代码中使用该变量,如以下示例所示:

$(document).ready(function(){
const width = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);
$(window).scroll(function() { 
if ($(document).scrollTop() > width) { 
$(".header").css("color", "#fff"); 
} else {
$(".header").css("color", "#000"); 
}
});
});

请注意,您指的是scrollTop因此您可能想要获取 vh,在这种情况下,您可以使用另一行代码:

const height = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);

最新更新