好的,我正在尝试将 CSS 样式应用于 <img>
没有任何 class
的标签或 id
分配给它。这是我的代码:
===CSS===
.tgll-navi { //begin custom class for navi bar
.navi-left { //.navi-left
float:right;
text-align:center;
} // end .navi-left
img { // img
max-height:269px !important;
max-width:524px !important;
height:75%;
width:75%;
padding:7px;
background-color:white;
border-style:solid;
border-color:black;
border-width:4px;
} // end img
.scale { // scale
max-width:174px !important;
max-height:89px !important;
height:25%;
width:25%;
padding:3px;
background-color:grey;
border-style:solid;
border-color:black;
border-width:2px;
}// end scale;
}// end tgll-navi class
=== jQuery 代码===
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 5) {
$('img[src="http://gourmetlunchladies.com/new/wp-content/uploads/2016/02/image3355.png"]').addClass(".scale");
} else {
$('img[src="http://gourmetlunchladies.com/new/wp-content/uploads/2016/02/image3355.png"]').removeClass(".scale");
}
});
我在 JavaScript console.log 中没有收到任何错误 如果我添加警报('消息');在 .addClass 之后,我得到了警报弹出窗口。 我删除了警报以查看该类(class)是否已应用,但该类(class)未被应用。
在带有 DMS2 的 WordPress 中工作,一切都更新到最新版本
了解我,我可能忘记了句号或分号
布伦特·希格斯
最佳答案
去掉“.”从您的 addClass 函数中,您不需要它;
...addClass(".scale")
成为
...addClass("scale")
关于javascript - 滚动上的 jQuery addClass 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35287279/