简体   繁体   English

如何在JS添加css类时应用css代码样式

[英]How to apply css code style when css class is added by JS

What's i'm trying to do is : i have a top fixed bar, that when scroll down it has a smaller height and the menu is ok, however the image logo scales under the black bar with about 5 px; 我要做的是:我有一个顶部固定条,当向下滚动它有一个较小的高度和菜单是好的,但图像标志在黑色条下缩放约5 px;

I thought to resolve this by: adding margin-top:30px to the logo, and i want when the class mk that changes the height of the bar to smaller one on scrolling, to make changes to the logo class to be margin-top:0 is this possible by JS? 我想通过以下方法来解决这个问题:在徽标上添加margin-top:30px ,我想要在滚动时将栏的高度更改为较小的类mk ,以使徽标类的更改为margin-top:0这可能是JS的margin-top:0吗?

In your logo maybe you can do something like? 在您的徽标中,您可以做类似的事情吗?

element.style.marginTop="0px"

Basically what you want to do is that when you reach the logic of scrolling in your JavaScript code you want to do 基本上你想要做的是当你达到你想要做的JavaScript代码滚动逻辑时

var element = document.getElementId("IdOfYourElementThatyouWantToApply");

Then you apply the style like the code above. 然后你应用上面代码的样式。

你可以在javascript中执行此操作:

yourObject.style.marginTop="0px"

In JQuery you can do: 在JQuery中,您可以:

$('#YourId').attr('style', 'margin-top:0px');

If you want to have the older style, just do something like that: 如果你想拥有旧款式,那就做一下这样的事情吧:

    var style = $('#YourId').attr('style');
style += " margin-top:0px;"
    $('#YourId').attr('style', style);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM