jQuery:vw而不是px

问题描述 投票:0回答:1

任何想法如何将100px转换为100vw?

$(document).ready(function(){
  $(window).scroll(function() { 
    if ($(document).scrollTop() > 100) { 
      $(".header").css("color", "#fff"); 
    } else {
      $(".header").css("color", "#000"); 
    }
  });
});
jquery pixel viewport
1个回答
0
投票

您可以通过此行代码获得视口宽度:

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);
© www.soinside.com 2019 - 2024. All rights reserved.