简体   繁体   English

如果超出视口,如何移动绝对定位元素?

[英]How can I move an absolute positioned element if out of viewport?

I have many boxes with differents widths. 我有很多不同宽度的盒子。 All of them are displayed inline-block so they will be distributed differently as window width changes. 所有这些都显示为inline-block因此随着窗口宽度的变化它们将以不同的方式分布。

Every box has a big tooltip that will show on hover. 每个盒子都有一个很大的工具提示,将在悬停时显示。 These tooltips have a fixed width and height, same for all of the boxes. 这些工具提示具有固定的宽度和高度,所有盒子都相同。

These tooltips are position:absolute over the boxes and centered with: 这些工具提示的position:absolute在方框上,并以:

left: 50%;
margin-left: -halfwidth; 

My problem is that if the box is close to window left or right they will be partially hidden (I have no problem with top and bottom, they will never reach those edges) 我的问题是,如果盒子靠近窗口向左或向右,它们将被部分隐藏(我没有顶部和底部的问题,它们永远不会到达那些边缘)

Is there any easy way with javascript (better if jquery) to detect when the element is out of the viewport and then change the left property accordingly to prevent it? 是否有任何简单的方法使用javascript(更好的是jquery)来检测元素何时离开视口然后相应地更改left属性以防止它?

any help, hint or comment will be greatly apreciated. 任何帮助,提示或评论都将大大贬值。 Thank You 谢谢

JSFIDDLE 的jsfiddle

 html, body {margin:0;padding:0;height:100%;} ul { width:100%; text-align:center; position:relative; top:50%; transform:translateY(-50%); padding:0; } li { display: inline-block; background-color: red; width: 100px; height: 25px; margin-right: 10px; position: relative; } .hover-element { position: absolute; background-color: yellow; z-index: 9999; width: 350px; height: 175px; left: 50%; margin-left: -175px; bottom:25px; display: none; border:1px solid #000; } li:hover .hover-element { display: block; } .hover-element:hover { visibility: hidden; } 
 <ul> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> </ul> 

Thats all you need, i detected the offset left position of the div, and added the class to li and a small tweaks in css. 多数民众赞成你需要的,我检测到div的偏移左侧位置,并将类添加到li和css中的小调整。

 $("li").mouseenter(function(){ var f = $(this).find('.hover-element'); var rightEdge = f.width() + f.offset().left; var leftEdge = f.offset().left; var screenWidth = $(window).width(); if(leftEdge < 0){ $(this).addClass("left"); $(this).removeClass("right"); } else if(rightEdge > screenWidth){ $(this).addClass("right"); $(this).removeClass("left"); } else{ $(this).removeClass("right lefft"); } }); $("li").mouseleave(function(){ $(this).removeClass("right lefft"); }) 
 html, body {margin:0;padding:0;height:100%;} ul { width:100%; text-align:center; position:relative; top:50%; transform:translateY(-50%); padding:0; } li { display: inline-block; background-color: red; width: 100px; height: 25px; margin-right: 10px; position: relative; } .hover-element { position: absolute; background-color: yellow; z-index: 9999; width: 350px; height: 175px; left: 50%; margin-left: -175px; bottom:25px; display: none; border:1px solid #000; } li.left .hover-element{ left: 0; margin-left: 0; } li.right .hover-element{ margin-left: 0; left: auto; right: 0; } li:hover .hover-element { display: block; } .hover-element:hover { visibility: hidden; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> </ul> 

Have a look: 看一看:

 var windowWidth = document.body.scrollWidth || window.innerWidth; $('li').on('mouseenter', function(e){ var $hover = $(this).find('.hover-element'); var coords = $hover[0] && $hover[0].getBoundingClientRect(); var rightDistance = 0; if (coords.left <= 0) { $hover.css({ 'left': ($hover.css('left').split('px')[0] - coords.left) + 'px' }) } rightDistance = windowWidth - coords.left - $hover.width() - 2 * $hover.css('borderWidth').split('px')[0]; if (rightDistance < 0) { $hover.css({ 'left': ($hover.css('left').split('px')[0] - (-rightDistance)) + 'px' }) } $('p').html($hover.css('left') + '/' + coords.left) }) 
 html, body {margin:0;padding:0;height:100%;overflow-x: hidden;} ul { width:100%; text-align:center; position:relative; top:50%; transform:translateY(-50%); padding:0; } li { display: inline-block; background-color: red; width: 100px; height: 25px; margin-right: 10px; position: relative; } .hover-element { position: absolute; background-color: yellow; z-index: 9999; width: 350px; height: 175px; left: 50%; margin-left: -175px; bottom:25px; display: none; border:1px solid #000; } li:hover .hover-element { display: block; } .hover-element:hover { visibility: hidden; } 
 <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <ul> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> <li> <div class="hover-element"></div> </li> </ul> 

The following links are a starting point for your question (if not the actual answer). 以下链接是您问题的起点(如果不是实际答案)。

Is there any easy way with javascript (better if jquery) to detect when the element is out of the viewport and then change the left property accordingly to prevent it? 是否有任何简单的方法使用javascript(更好的是jquery)来检测元素何时离开视口然后相应地更改左属性以防止它?

How to tell if a DOM element is visible in the current viewport? 如何判断DOM元素在当前视口中是否可见?

Absolute position of an element on the screen using jQuery 使用jQuery在屏幕上的元素的绝对位置

I'm going to mention the steps here. 我要提到这里的步骤。 If you'd need the code, just ask for it. 如果您需要代码,只需要它。 I am currently on my mobile device, so typing the code would get hard! 我目前在移动设备上,所以输入代码会很难!

Steps 脚步

  • To make things easy to include JS, change the display property using JS. 为了简化包含JS,使用JS更改display属性。 Accomplish this using a mouse event on each of the list items, that changes their respective tooltip's display property. 使用每个列表项上的鼠标事件来完成此操作,这会更改其各自的工具提示的显示属性。 ( Here's how to access the respective child) 以下是如何访问相应的孩子)

In the same hover event, do these: 在同一个悬停事件中,执行以下操作:

  • After changing the display, check if the tooltip element is in viewport. 更改显示后,检查工具提示元素是否在视口中。 You can refer to the link Victor Medeiros provided. 您可以参考Victor Medeiros提供的链接。

  • If is not completely in viewport, change the margin-left property to -175px - (the number of pixels it crossed the boundary by) . 如果未完全在视口中, 请将margin-left属性更改为 -175px - (the number of pixels it crossed the boundary by) I just realised, if it crosses the viewport, just set the margin-left or margin-right (as applicable, find it by getBoundingClientRect) to 0. 我刚刚意识到,如果它穿过视口,只需将margin-left或margin-right(如果适用,通过getBoundingClientRect找到它)设置为0。

Yep, that's it! 是的,就是这样! I hope that should work, I haven't tried it out. 我希望这应该有效,我还没试过。 What's the result? 结果是什么?

Change 更改

li {
    display: inline-block;
    background-color: red;
    width: 100px;
    height: 25px;
    margin-right: 10px;
    position: relative;
}

Whit 惠特

li {
    display: inline-block;
    background-color: red;
    width: 100px;
    height: 25px;
    margin-right: 10px;
    position: static;
}

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

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