Jquery - 悬停时概述顶部元素
我试图勾勒出悬停在顶部的元素。
$(function()
{
var ElementSelector = null;
var SelectedElement = null;
$("*:not(div#ElementSelector)").hover
(
function()
{
SelectedElement = ($(this).length > 1 ? $(this).find("*:last") : $(this));
if(ElementSelector != null)
{
ElementSelector.css("width", SelectedElement.width()).css("height", SelectedElement.height())
.css("left", SelectedElement.offset().left).css("top", SelectedElement.offset().top)
.css("marginTop", SelectedElement.css("marginTop")).css("marginLeft", SelectedElement.css("marginLeft"))
.css("marginRight", SelectedElement.css("marginRight")).css("marginBottom", SelectedElement.css("marginBottom"))
.css("paddingLeft", SelectedElement.css("paddingLeft")).css("paddingTop", SelectedElement.css("paddingTop"))
.css("paddingRight", SelectedElement.css("paddingRight")).css("paddingBottom", SelectedElement.css("paddingBottom"));
}
SelectedElement.click(function()
{
SelectedElement.css("background-color", "green");
});
},
function ()
{
SelectedElement.unbind("click");
}
);
$("body").append("<div id='ElementSelector' style='width:0px;height:0px;z-index: 100000;position:absolute;border: 2px solid red;padding:0px;margin:0px;margin:0px;'></div>");
ElementSelector = $("div#ElementSelector");
});
这是我到目前为止所想出的..但它有很多错误! 那么有什么想法吗?
I'm trying to outline the top element being hovered.
$(function()
{
var ElementSelector = null;
var SelectedElement = null;
$("*:not(div#ElementSelector)").hover
(
function()
{
SelectedElement = ($(this).length > 1 ? $(this).find("*:last") : $(this));
if(ElementSelector != null)
{
ElementSelector.css("width", SelectedElement.width()).css("height", SelectedElement.height())
.css("left", SelectedElement.offset().left).css("top", SelectedElement.offset().top)
.css("marginTop", SelectedElement.css("marginTop")).css("marginLeft", SelectedElement.css("marginLeft"))
.css("marginRight", SelectedElement.css("marginRight")).css("marginBottom", SelectedElement.css("marginBottom"))
.css("paddingLeft", SelectedElement.css("paddingLeft")).css("paddingTop", SelectedElement.css("paddingTop"))
.css("paddingRight", SelectedElement.css("paddingRight")).css("paddingBottom", SelectedElement.css("paddingBottom"));
}
SelectedElement.click(function()
{
SelectedElement.css("background-color", "green");
});
},
function ()
{
SelectedElement.unbind("click");
}
);
$("body").append("<div id='ElementSelector' style='width:0px;height:0px;z-index: 100000;position:absolute;border: 2px solid red;padding:0px;margin:0px;margin:0px;'></div>");
ElementSelector = $("div#ElementSelector");
});
This is what I've come up with so far.. But it bugs a lot! So any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用CSS和jQuery的addClass、removeClass和toggleClass函数而不是在代码中指定所有样式呢? 这应该会大大清理您的代码,并将演示文稿放在它所属的位置 - 样式表中。
Instead of specifying all the styles in your code why not use CSS and jQuery's addClass, removeClass, and toggleClass functions? This should clean up your code quite a bit and put the presentation where it belongs - in the style sheets.
您始终可以使用 .hover 吗?
我强烈建议清理您的代码并按照马克所说的操作并将样式放入样式表中。 它只有在使用 jQuery 时才有意义,它可能会变得非常混乱。
如果您第一次就以正确的方式完成了它,那么测试您的代码会容易得多,并且您将有一个更好的基础来开始,事实上您的“测试”可能会成为最终产品。
整个追加事情没有任何意义,为什么不使用 jQuery 的 CSS 函数 设置它? 这就是他们存在的目的。
You can always use .hover?
I would heavily suggest cleaning up your code and doing what Mark said and putting the style within the style sheets. It only makes sense when working with jQuery, it can get to be quite a mess.
If you would have done it the right way the first time around, testing your code would be much easier and you would have a much better base to start from, in fact your 'test' would probably become the end product.
The whole append thing doesn't make any sense, why don't you set it using jQuery's CSS functions? That's what they're there for.