gpt4 book ai didi

javascript - 当我们使用 JavaScript 更改元素类时如何将其他样式设置为默认样式

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:07 27 4
gpt4 key购买 nike

我已经创建了 div 并将其放置在页面的右边距 position:fixed。当我调用 JavaScript 函数时,我需要将 div 放在底部边距。我尝试使用 classList.add()classList.remove() 来完成我的任务。

这是我创建的 CSS 类,

.example-right{
position:fixed;
top:50px;
right:0px;
}
.example-bottom{
position:fixed;
left:50px;
bottom:0px;
}

这是我试过的 JavaScript 代码,

function test(){
document.getElementById('target').classList.remove('example-right');
document.getElementById('target').classList.add('example-bottom');
}

当我触发 test() 函数时,它会删除 example-right 并将其替换为 example-bottom。但是 div 的 top 值在函数被触发后不会改变。因此,它不会将 div 放在窗口的底部边缘。我可以将 top:## px; 放到 example-bottom 中。但是当页面调整大小时

是否可以使用 CSS 解决此问题。我知道我可以使用 document.getElementById('target').style.top = window.innerHeight- div_Height 更改顶部值。但我想知道有没有办法使用 css 或其他方法来做到这一点。 (否则当我需要改变位置时,我必须一直使用 JavaScript)

仅限纯 CSS 和 JavaScript

最佳答案

你。做对了。唯一的事情就是多了解一点 css。

.example-right{
position:fixed;
top:50px;
right:0px;
}
.example-bottom{
position:fixed;
top:inital
left:50px;
bottom:0px;
}

关于javascript - 当我们使用 JavaScript 更改元素类时如何将其他样式设置为默认样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300902/

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