简体   繁体   English

将放置按钮放置在位置为相对的元素的右上角

[英]Place Button at top right corner of an element with position:relative

I wrote this script: 我写了这个脚本:

var HTML_BT = '<a class="helper" href="#"><i class="icon-wrench"></i></a>';

// Append button
$("my_selector").live('mouseenter', function(){
    var 
        bt = $(this).find('a.helper'),
        pos = $(this).position();

    // Check if the button exists and creates it
    if (bt.length==0){
        bt = $(HTML_BT);
        bt.css({
            position:'absolute',

            // Calculates coordinates
            top:pos.top + 15 + 'px', 
            left:pos.left + $(this).width() - 15 + 'px'

            // .. Some other css like border, bg, color and so on.
        });
        $(this).append(bt);
    }

    // Show the button
    bt.show();

}).live('mouseleave', function(){
    var 
        bt = $(this).find('a.helper');

    // Show the button if exists
    if (bt.length!=0){
        bt.hide();
    }
});

The script shows or appends a link at top/right corner, when the mouse cursor goes on a specific item. 当鼠标光标移到特定项目上时,脚本会在顶部/右上角显示或附加一个链接。

It works fine, but I have some troubles calculating the top and right coordinates on elements placed inside containers that has specified the css position as relative, because the link coordinates are (rightly) calculated as relative of his container. 它可以正常工作,但是我在计算将css位置指定为相对的容器内放置的元素的顶部和右侧坐标时遇到了一些麻烦,因为链接坐标(正确地)被计算为其容器的相对位置。

.carousel-inner{
    position:relative;
}

Here I did a working example: http://jsfiddle.net/ucfKm/ 在这里,我做了一个工作示例: http : //jsfiddle.net/ucfKm/

Someone knows how to test if I have to use absolute / relative coordinates or how to get the right left position? 有人知道如何测试我是否必须使用绝对/相对坐标,或者如何获得右左位置?

Thanks a lot, Davide. 非常感谢Davide。

Instead of calculating the left position to put it on the right and the top position just remember that an absolutely positioned element inside a relatively positioned element will have its 0,0 at the top left corner of the parent, so: 请记住,相对定位的元素内绝对定位的元素将在父元素的左上角具有0,0,而不是计算将其置于右侧和顶部的左位置。

top:15 + 'px', 
right:15 + 'px',

Will position your a element at 15 px from the top of the parent and 15px from the right of the parent. 将您的元素放置在距离父级顶部15像素和距离父级右侧15像素的位置。

Fiddle update: http://jsfiddle.net/ucfKm/5/ 小提琴更新: http//jsfiddle.net/ucfKm/5/

EDIT: Also, note that because you dont have to calculate the position in this case, you can assign the css directly to the class on your css file, and avoid unnecessary javascript logic. 编辑:另外,请注意,由于在这种情况下不必计算位置,因此可以将css直接分配给css文件上的类,并避免不必要的javascript逻辑。

position: relative; does not position the element relative to the parent. 不会相对于父元素放置元素。 It positions the element relative to that element's original position. 它将元素相对于该元素的原始位置定位。

If you want the element to be positioned 15px from the top and right of the parent (as long as the parent has position: set on itself with a value of either relative , absolute , or fixed ), use: 如果要将元素放置在距离父级顶部和右侧15像素的位置(只要父级的position:设置在其自身上,其值可以是relativeabsolutefixed ),请使用:

.carousel-inner{
    position: absolute;
    top: 15px;
    right: 15px;
}

If you want the element to be positioned 15px from the top and right of the entire page, use: 如果您希望元素位于整个页面顶部和右侧15像素的位置,请使用:

.carousel-inner{
    position: fixed;
    top: 15px;
    right: 15px;
}

In either case, you have no need of doing javascript position calculations. 无论哪种情况,您都无需进行javascript位置计算。

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

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