如何在嵌套 jQuery 函数中更改 css
我正在尝试创建一个元素,当鼠标悬停在其图像上时,其图像会发生变化,并且其位置也会相应调整。然而,图像正在改变,但 css 位置没有改变。
jQuery/JavaScript:
var newComment = $('<span>').attr({'id': commentCount});
newComment
.addClass('comment')
.css({
'top': (yOffset * 100) + 175 + "px",
'left': (xOffset * 75) + 40 + "px"
})
.hover(function(){ //Hover over the comment
newComment
.removeClass()
.addClass('commentOver')
.offset({
'top': yOffsets[newComment.id] + 175 - 1250 + "px",
'left': xOffsets[newComment.id] + 40 - 1500 + "px"
});
},function(){ //Mouse leaves the comment
newComment
.removeClass()
.addClass('comment')
.offset({
'top': yOffsets[newComment.id] + 1750 + "px",
'left': xOffsets[newComment.id] + 400 + "px"
});
});
CSS:
.comment{
position: absolute;
z-index: 10;
padding: 0px;
width: 51px;
height: 70px;
background-image: url('../img/dropSmall.png');
font-weight:800;
}
你能看出我哪里出错了吗?为什么?
I'm trying to create an element which when the mouse is over its image changes and it's position is adjusted accordingly. However the image is changing but the css position isn't changing.
jQuery/JavaScript:
var newComment = $('<span>').attr({'id': commentCount});
newComment
.addClass('comment')
.css({
'top': (yOffset * 100) + 175 + "px",
'left': (xOffset * 75) + 40 + "px"
})
.hover(function(){ //Hover over the comment
newComment
.removeClass()
.addClass('commentOver')
.offset({
'top': yOffsets[newComment.id] + 175 - 1250 + "px",
'left': xOffsets[newComment.id] + 40 - 1500 + "px"
});
},function(){ //Mouse leaves the comment
newComment
.removeClass()
.addClass('comment')
.offset({
'top': yOffsets[newComment.id] + 1750 + "px",
'left': xOffsets[newComment.id] + 400 + "px"
});
});
CSS:
.comment{
position: absolute;
z-index: 10;
padding: 0px;
width: 51px;
height: 70px;
background-image: url('../img/dropSmall.png');
font-weight:800;
}
Can you see where i'm going wrong and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
newComment 范围是否已添加到文档中?
由于我们没有其余的 HTML/CSS/代码,我不知道这些是否是唯一的问题,但我建议使用 $(this) 并在 removeClass() 中传递参数:
我不请参阅 jQuery 文档,您可以在不传递任何参数的情况下调用removeClass()。它说您需要一门或多门课程。
此外,如果单位是 px,则 offset() 方法不需要单位。
您可能还想查看 toggleClass("a", "b"),这很容易交换两个班级。
Has the newComment span been added to the document?
Since we don't have the rest of your HTML/CSS/code, I don't know if these are the only issues, but I would suggest using $(this) and passing a parameter in removeClass():
I don't see in the jQuery documentation where you can call removeClass() with no parameters passed. It says you need one or more classes.
In addition the offset() method does not need units if the units are px.
You may also want to look at toggleClass("a", "b"), which makes it easy to swap two classes.
您是否定位了元素
newComment
absolute
?如果元素定位为
静态
,则不能使用顶部和左侧。如果没有,请尝试使用此方法来代替当前的
.css
:顺便说一句:当您在 DOM 中编写时,不必将 css 属性的名称作为字符串传递。所以这应该是一样的:
Did you position the element
newComment
absolute
?You can't use top and left if the element is positioned
static
.If you didn't, try this instead of your current
.css
:By the way: You don't have to pass the name of the css property as a string, when you write in DOM like. So this should work just the same:
删除 offset 对象参数末尾的“px”;)
如果 offset 方法无法将
left
和top
属性识别为数字,则不会更新元素位置。remove the "px" at the end of the offset object parameter ;)
If the offset method does not recognize the
left
andtop
properties as a number, it does not update the element position.