gpt4 book ai didi

javascript - 通过字符串分配translateY值

转载 作者:行者123 更新时间:2023-12-01 03:55:18 26 4
gpt4 key购买 nike

当用户滚动页面以及接近页面底部时,我使用一些 JavaScript 来保持侧边栏固定,然后使侧边栏 position: static 。这是代码:

<script>

window.onscroll = function() {
var px = window.scrollY;
var x = document.getElementsByClassName("sidebar");
var body = document.body,
html = document.documentElement;
var poht = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
var poht2 = poht - 1300;

if(px < poht2){

if(px > 1700)
{x[0].style.position="fixed";x[0].style.transform="translateY(-1700px)";}

if(px < 1680){
if(x[0].style.position=="fixed")
{x[0].style.position="static";x[0].style.transform="translateY(20px)";}
}
}
// Problem occurs here

pos2 = -1 * 2300 - poht2;
pos2 = pos2 + "px";

else {x[0].style.position="static";x[0].style.transform="translateY(pos2)";}

}
</script>

在最后一行(我的意思是else),当我想计算一个值并将其分配给translateY()时,它不起作用。这样做的正确形式是什么?

谢谢。

最佳答案

您可以使用字符串连接或模板文字

x[0].style.transform = "translateY(" + pos2 + ")"

x[0].style.transform = `translateY(${pos2})`

关于javascript - 通过字符串分配translateY值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42743776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com