我正在使用 Wordpress 以及在 .scroll jQuery 函数中用“%”替换“px”的内容。这是使用 'px' 选项工作的代码(在 http://robo.im ) -
<script>
jQuery(document).ready(function($) {
$(window).scroll(function () {
if ($(window).scrollTop() > 200) {
$('.home #masthead').css("opacity", 0.98);
}
else{
$('.home #masthead').css("opacity", 0);
}
});
});
</script>
最佳答案
您可以通过滚动距离和总高度来计算百分比
jQuery(document).ready(function($) {
$(window).scroll(function () {
var height = Math.max.apply(null, [$('body').prop('scrollHeight'),$('html').prop('scrollHeight')]),
percent = $(window).scrollTop() / (height - $(window).height()) * 100;
if (percent > 20) {
$('.home #masthead').css("opacity", 0.98);
}
else{
$('.home #masthead').css("opacity", 0);
}
});
});
关于javascript - 如何在 .scroll jQuery 函数中将 'px' 替换为 '%',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816812/