【问题标题】:How to make a div be to the right of selected element?如何使 div 位于所选元素的右侧?
【发布时间】:2015-04-15 00:02:58
【问题描述】:

我有两个面板,左侧和右侧。一旦用户将鼠标悬停在右侧面板的任何元素上,一个新的 div(称为框)应该在悬停元素的右侧可见。

我可以设计它,但问题是宽度是固定的,弹出面板的位置会根据屏幕大小而变化。它在 JSFiddle 上不能正常工作,所以我没有把它放在那里,但是,提供了在任何浏览器中都可以正常工作的完整代码。

  • 请注意两个面板都在一个“容器”中。
  • 即使屏幕尺寸发生变化,红框也应保留在右侧面板上。 (屏幕截图中显示的位置)
  • 由于屏幕小,移动设备不会显示红色框。

职位

Left Panel Right Panel
Element1  |box1
Element2  |box2

框元素与右侧面板重叠。

代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
          <div class="container">
        <div class="row">
            <div class="col-md-7" style="background-color: blue;">
                <div class="row">
                    <div class="col-md-12" style="padding-right: 0;">
                            <div class="myTarget" style="background-color: white;">
                                <p>This is element 1 of panel left </p>
                                                                <p>This is element 1 of panel left </p>
                                                                <p>This is element 1 of panel left </p>
                                                                <p>This is element 1 of panel left </p>
                                                                <p>This is element 1 of panel left </p>
                                                                <p>This is element 1 of panel left </p>
                                                                <p>This is element 1 of panel left </p>
                                                                <p>This is element 1 of panel left </p>
                                                                <p>This is element 1 of panel left </p>
                                <div id="box" class="col-md-8 hidden-xs"
                                    style="display: none; z-index: 1000; background-color: red; border-color: blue;">
                                    <p>This is the third panel of element 1 that need to be re-positioned.</p>
                                </div>
                            </div>
                                                        <div class="myTarget" style="background-color: white;">
                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                                                <p>This is element 2 of panel left</p>
                                <div id="box" class="col-md-8 hidden-xs"
                                    style="display: none; z-index: 1000; background-color: red; border-color: blue;">
                                    <p>This is the third panel of element 2 that need to be re-positioned.</p>
                                </div>
                            </div>
                    </div>
                </div>
            </div>
            <div id="rightPanel" class="col-md-4" style="background-color:green;padding-left: 0">
                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
            </div>
          </div>
    </div>
    <script>
                $(".myTarget").hover(function() {
            var temp= $(this).children("#box");
            var tempLeft = $('#rightPanel').offset().left;
            temp.css({
                display : "block",
                position : "absolute",
                left : tempLeft + "px",
                top : 0 + "px",
            });
        }, function() {
            $(this).children("#box").hide();
        });
    </script>
</body>
</html>

我想这就是丹尼尔想要的

没有元素被悬停

元素1被悬停,所以元素1的红色框会显示在它的右边。

元素2被悬停,所以元素2的红色框会显示在它的右边。

【问题讨论】:

  • 您可以在使其可见时通过 Javascript 定位它,然后在窗口大小发生变化时重新定位它(导致重新布局)。或者,您可以使用 CSS 样式将其相对于其他元素定位。
  • @jfriend00 我认识哥们,但不知道如何
  • 然后问一个更具体的问题,关于您究竟需要什么帮助。你的问题太模糊了,因为有很多不同的方法取决于你想要做什么以及你想要它定位的确切方式、你想要它定位的时间、它是否与其他事物重叠等等。我建议你问一个更具体的问题,你更有可能得到一个详细的答案。您评论中的“伙伴”参考并没有很好地体现出来。我试图帮助你。如果您不希望我提供帮助,我可以很乐意花时间处理其他问题而忽略您的问题。
  • @jfriend00 误会了抱歉,无论如何我更新了问题,如果仍然不清楚,请告诉我改写它
  • @jfriend00 框将与右侧面板重叠。我不关心 js 和静态选项,我想要的只是确保它适用于每个屏幕尺寸。 (手机除外)

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

首先,您有两个具有相同 ID 的元素。这很糟糕,所以我将它们更改为类。我已经删除了讨厌的内联样式并使用了一些正确的 CSS 声明。我还把你的盒子改成了跨度。你马上就会明白为什么。

如果下面的代码片段不起作用,这里是JSFiddle

据我了解,您希望您的 box 相对于您的 p 标签定位。基于此,我建议将您的 box 元素更改为 span,然后使用 jQuery clone() 函数,如下所示:

$('.myTarget').on('mouseover', 'p', function () {
    $(this).parent().children('.box').clone().appendTo($(this));    
});
$('.myTarget').on('mouseout', 'p', function () {
    $(this).children('.box').remove(); 
});
.col-md-7 {
    background-color: blue;
}
.col-md-12 {
    padding-right: 0;
}
#rightPanel {
    background-color:green;
    padding-left: 0;
}
.myTarget {
    background-color: white;
}
.box {
    display:none;
    z-index: 1000;
    background-color: red;
    border-color:blue;
}

.myTarget p {
    position:relative;
}
.myTarget p .box {
    display: block;
    position: absolute;
    left: 100%;
    top: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<div class="container">
    <div class="row">
        <div class="col-md-7">
            <div class="row">
                <div class="col-md-12">
                    <div class="myTarget">
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <div class="box" class="col-md-8 hidden-xs">
                            <p>This is the third panel of element 1 that need to be re-positioned.</p>
                        </div>
                    </div>
                    <div class="myTarget">
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <span class="box" class="col-md-8 hidden-xs">
                            <p>This is the third panel of element 2 that need to be re-positioned.</p>
                        </span>
                    </div>
                </div>
            </div>
        </div>
        <div id="rightPanel" class="col-md-4">
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
        </div>
    </div>
</div>

您可以使用.myTarget p .box 的 CSS 定位来将其定位到您认为合适的位置。

编辑 1

现在您已经阐明了您对图像的需求,我已经更新了代码。评论应该解释它,这是一个fiddle,以防 sn-p 不起作用:

// Loop through each box and set the width to the same as #rightPanel
$('.box').each(function() {
   var newWidth = $('#rightPanel').outerWidth();
   $(this).outerWidth(newWidth); 
});
.col-md-7 {
    background-color: blue;
}
.col-md-12 {
    padding-right: 0;
}
#rightPanel {
    background-color:green;
    padding-left: 0;
}
.myTarget {
    background-color: white;
    position:relative;
}
.box {
    display:none;
    z-index: 1000;
    background-color: red;
    border-color:blue;
  
    /* Position the box to the top right of .myTarger */
    position: absolute;
    left: 100%;
    top: 0;
}
.myTarget:hover .box { /* Show the box on hover */
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<div class="container">
    <div class="row">
        <div class="col-md-7">
            <div class="row">
                <div class="col-md-12">
                    <div class="myTarget">
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <p>This is element 1 of panel left</p>
                        <div class="box" class="col-md-8 hidden-xs">
                            <p>This is the third panel of element 1 that need to be re-positioned.</p>
                        </div>
                    </div>
                    <div class="myTarget">
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <p>This is element 2 of panel left</p>
                        <span class="box" class="col-md-8 hidden-xs">
                            <p>This is the third panel of element 2 that need to be re-positioned.</p>
                        </span>
                    </div>
                </div>
            </div>
        </div>
        <div id="rightPanel" class="col-md-4">
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
            <p>This is panel right</p>
        </div>
    </div>
</div>

【讨论】:

  • 谢谢 Jamie,问题是盒子随着鼠标坐标移动,我只需要固定盒子的位置和用户悬停的左面板元素的大小.原因是如果您在元素上移动鼠标,它会导致红色框随着位置的变化而闪烁。
  • 我假设 Daniel Newtown 和 @Jack 是同一个人?我已经根据新信息更新了答案。
  • @JamieBarker :D 没必要成为一个,我有类似的问题,所以我为这个问题提供了赏金。
  • @JamieBarker 谢谢你的回答,我会给你赏金。
【解决方案2】:

您想将框放置在右侧并与 myTarget div 垂直对齐吗? 在您的脚本中再添加一行并查看。

$(".myTarget").hover(function() {
   var box_top = $(this).position().top;
   var temp= $(this).children("#box");
   var tempLeft = $('#rightPanel').offset().left;
   temp.css({
      display : "block",
      position : "absolute",
      left : tempLeft + "px",
      top : box_top + "px",
   });
}, function() {
  $(this).children("#box").hide();
});

元素 1 鼠标悬停

元素 2 鼠标悬停

【讨论】:

  • 谢谢,如何让红框更靠近左侧面板?如问题框的“位置”部分所示,应该是右侧面板所在的位置。
【解决方案3】:

我刚刚复制了您的代码并进行了更改。
将元素的所有 css 移动到 css 文件中!!!
将 Box 从 id 更改为 class。
将 leftPanels 位置设置为相对。
设置框 css: position:absolute;显示:无;z-index:1000;左:100%;
简单地在鼠标悬停时显示当前“myTarget”中的“框”。
如果需要,请将 with 设置为“rightPanel”的 with!

代码如下:

            <!DOCTYPE html>
            <html lang="en">
                <head>
                    <meta name="viewport" content="width=device-width, initial-scale=1">
                    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
                    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
                </head>
                <body>
                    <div class="container">
                        <div class="row">
                            <div class="col-md-7" style="background-color: blue;">
                                <div class="row">
                                    <div class="col-md-12" style="padding-right: 0;">
                                        <div class="myTarget" style="background-color: white;position: relative;">
                                            <p>This is element 1 of panel left </p>
                                            <p>This is element 1 of panel left </p>
                                            <p>This is element 1 of panel left </p>
                                            <p>This is element 1 of panel left </p>
                                            <p>This is element 1 of panel left </p>
                                            <p>This is element 1 of panel left </p>
                                            <p>This is element 1 of panel left </p>
                                            <p>This is element 1 of panel left </p>
                                            <p>This is element 1 of panel left </p>
                                            <div class="box" class="col-md-8 hidden-xs"
                                                 style="position: absolute;top:0px; left:100%; display: none; z-index: 1000; background-color: red; border-color: blue;">
                                                <p>This is the third panel of element 1 that need to be re-positioned.</p>
                                            </div>
                                        </div>
                                        <div class="myTarget" style="background-color: white; position: relative;">
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <p>This is element 2 of panel left</p>
                                            <div class="box" class="col-md-8 hidden-xs"
                                                 style="position:absolute; top:0px; left:100%;  display: none; z-index: 1000; background-color: red; border-color: blue;">
                                                <p>This is the third panel of element 2 that need to be re-positioned.</p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div id="rightPanel" class="col-md-4" style="background-color:green;padding-left: 0">
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                                <p>This is panel right</p>
                            </div>
                        </div>
                    </div>
                    <script>
                        $(function(){//start of load
                            //This only works if box is inside leftPanel, and leftPanel have position absolute or relative!
                            //Set box "position:absolute;display:none;z-index:9999;top:0" with css
                            $(".myTarget").hover(function() {
                               //CHANGE "box" id to class!!
                               var box = $(this).children(".box")
                               box.show();
                               //If you want dynamic resize of with:
                               box.css("width", $("#rightPanel").outerWidth());
                            }, function() {
                              $(".box").hide();//hide all "box" class elements
                            });
                        });
                    </script>
                </body>
            </html>

【讨论】:

    【解决方案4】:

    您可以获取鼠标的位置并使用该信息来放置第二个 div。


        getMousePosition: function(){ 
            var xOffset = event.pageX;
            var yOffset = event.pageY;
            //////////////console.log("xOffset: " + xOffset);
            //////////////console.log("yOffset: " + yOffset);
        }, //. getMousePosition
    

    或者您可以在一开始将 div 放置在正确的位置,然后将“display:none”应用于第二个 div。然后只需将其打开“显示:块;”需要时。如果需要,在调用时使用 ajax 插入的内容填充 div2。

    或者您可以获取 div 1 左上角的位置,并使用该信息通过 javascript 调整将 div 2 放置到正确的位置。

    解决这个问题的方法很多。

    【讨论】:

    • 使用鼠标位置不起作用,因为框的位置是基于元素而不是鼠标,鼠标指针可以在所选元素内的任何坐标。
    • 我提供了三个选项,我很清楚鼠标位置是相对的,而不是精确到 div 的位置。但这可能会帮助他思考解决方案。我经常将鼠标位置用于不需要精确位置的工具提示。不要认为我应该因为提供替代品而受到批评。 :-)
    【解决方案5】:

    你的问题很奇怪!您谈论选择(没有选择任何内容,悬停与选择不同!),以及某种长度? (你是说宽度吗?)

    无论如何,你可以用 js 定位任何你喜欢的东西: 在悬停功能中,您可以找到当前“.myTarget”的所有位置,将this 更改为“#rightPanel”,您就拥有了 rightPanel 的所有位置:

    var Top = $(this).offset().top;
    var Left = $(this).offset().left;
    var Width = $(this).outerHeight();
    var Height = $(this).outerHeight();
    var Bottom = Top + Height;
    var Right = Left + Width;
    

    注意:如果你想在调整窗口大小时改变它,你需要tab进入resize事件:

    $(window).resize(function(){
        //do stufff
    })
    

    【讨论】:

    • @DanielNewtown 我刚刚给你的“上”和“右”,然后在你的悬停函数中写下这个: $("#rightPanel").css({top: Top, left:对 });
    • 您对 Jamies 的回答有何看法?
    • Jamies 首先将“悬停”事件从“leftPanel”移动到“leftPanel”的“p”标签,所以如果这就是你想要的,那就找到它。这意味着“box”将在“p”标签的左边,而不是“leftPanel”!但随后他创建了一个“盒子”的克隆,在每次鼠标悬停时将其放入“p”标签中,然后在鼠标移出时将其销毁。我应该这样做不同!它也似乎是有线的!他是正确的,因为你不能在两个“box”元素上拥有相同的 id!为什么它们在 leftPanels 之内,即使您希望它们位于外部。 (不是你做不到,而是为什么呢?)
    猜你喜欢
    • 1970-01-01
    • 2014-10-26
    • 2016-03-21
    • 1970-01-01
    • 2021-04-09
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多