如何在嵌套 jQuery 函数中更改 css

发布于 2024-11-28 05:46:16 字数 1430 浏览 3 评论 0原文

我正在尝试创建一个元素,当鼠标悬停在其图像上时,其图像会发生变化,并且其位置也会相应调整。然而,图像正在改变,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-12-05 05:46:16

newComment 范围是否已添加到文档中?

由于我们没有其余的 HTML/CSS/代码,我不知道这些是否是唯一的问题,但我建议使用 $(this) 并在 removeClass() 中传递参数:

    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                       
            $(this)
            .removeClass("comment")
            .addClass('commentOver')
            .offset({
                'top': yOffsets[newComment.id] + 175 - 1250,
                'left': xOffsets[newComment.id] + 40 - 1500
            });

        },function(){ //Mouse leaves the comment               
            $(this)
            .removeClass("commentOver")
            .addClass('comment')
            .offset({
                'top': yOffsets[newComment.id] + 1750,
                'left': xOffsets[newComment.id] + 400" 
            });                      
        });   

我不请参阅 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():

    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                       
            $(this)
            .removeClass("comment")
            .addClass('commentOver')
            .offset({
                'top': yOffsets[newComment.id] + 175 - 1250,
                'left': xOffsets[newComment.id] + 40 - 1500
            });

        },function(){ //Mouse leaves the comment               
            $(this)
            .removeClass("commentOver")
            .addClass('comment')
            .offset({
                'top': yOffsets[newComment.id] + 1750,
                'left': xOffsets[newComment.id] + 400" 
            });                      
        });   

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.

泼猴你往哪里跑 2024-12-05 05:46:16

您是否定位了元素 newComment absolute
如果元素定位为静态,则不能使用顶部和左侧。

如果没有,请尝试使用此方法来代替当前的 .css

.css({
      'top': (yOffset * 100) + 175 + "px",
      'left': (xOffset * 75) + 40 + "px",
      'position': 'absolute'
})

顺便说一句:当您在 DOM 中编写时,不必将 css 属性的名称作为字符串传递。所以这应该是一样的:

.css({
     top: (yOffset * 100) + 175 + "px",
     left: (xOffset * 75) + 40 + "px",
     position: 'absolute'
})

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:

.css({
      'top': (yOffset * 100) + 175 + "px",
      'left': (xOffset * 75) + 40 + "px",
      'position': 'absolute'
})

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:

.css({
     top: (yOffset * 100) + 175 + "px",
     left: (xOffset * 75) + 40 + "px",
     position: 'absolute'
})
青巷忧颜 2024-12-05 05:46:16

删除 offset 对象参数末尾的“px”;)

.offset({
                'top': yOffsets[newComment.id] + 175 - 1250,// + "px",
                'left': xOffsets[newComment.id] + 40 - 1500// + "px"
            });

如果 offset 方法无法将 lefttop 属性识别为数字,则不会更新元素位置。

remove the "px" at the end of the offset object parameter ;)

.offset({
                'top': yOffsets[newComment.id] + 175 - 1250,// + "px",
                'left': xOffsets[newComment.id] + 40 - 1500// + "px"
            });

If the offset method does not recognize the left and top properties as a number, it does not update the element position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文