简体   繁体   English

当指针位于页面右侧时,将自动重新对齐工具提示

[英]automatically realign the tooltip when the pointer is on the right side of the page

how to make the tooltip automatically place to the left side of the pointer when the pointer hovering the right side of the page? 当指针悬停在页面右侧时,如何使工具提示自动放置在指针左侧?

here is my html 这是我的html

<div class="container">
<div class="img">
<a href="html/takraw.html" class="tooltip">
<img class="img" src="images/sepak-takraw.jpg" alt="img">
<span>
  <div class="tooltipbox">
    <h2>Sepak Takraw</h2>
  </div>
 </span>
 </a>
 <div class="desc">Sepak Takraw</div>
 </div>
 <div class="img">
  <a href="#" class="tooltip">
  <img src="images/archery.jpg" alt="img"/>
   <span>
   <div class="tooltipbox">
    <h2>Archery</h2>
  </div>
   </span>
  </a> 
  <div class="desc">Archery</div>
  </div>
  </div>

style.css style.css

div.img {
margin: 5px;
border: 1px solid #ccc;
float: left;
width: 240px;
} 
div.img img {
width: 240px;
height:200px;
}
.container {
background:white; padding: 20px; 
position: relative;
margin: auto;
border-radius: 5px;
}
.tooltipbox {
width: 400px;
min-height: 400px;
background: white;
opacity: .95;
padding: 20px;
}
.tooltip {
text-decoration:none;
position:relative;      
}
.tooltip span {
display:none;
}
.tooltip:hover span {
display:block;
position:fixed;
overflow:hidden;
z-index: 9999;
}

script.js script.js

var tooltips = document.querySelectorAll('.tooltip span');

window.onmousemove = function (e) {
var x = (e.clientX + 20) + 'px',
    y = (e.clientY + 20) + 'px';


for (var i = 0; i < tooltips.length; i++) {
    tooltips[i].style.top = y;
    tooltips[i].style.left = x;
}  
};

I want to make my tooltip to place to the left side of the the pointer when the pointer hover the right side of the page. 当指针悬停在页面的右侧时,我想使工具提示放置在指针的左侧。 how to do this? 这个怎么做?

JSFIDDLE : https://jsfiddle.net/t5atf4z1/ JSFIDDLE: https ://jsfiddle.net/t5atf4z1/

You should check tooltips right side position is out of document. 您应该检查工具提示右侧位置没有文档。

like this 像这样

tooltips[i].style.left = ((e.clientX + 20) + tooltips[i].clientWidth < document.body.clientWidth)?x:(e.clientX + 20)-tooltips[i].clientWidth+'px';

Working fiddle 工作提琴

 var tooltips = document.querySelectorAll('.tooltip span'); window.onmousemove = function (e) { var x = (e.clientX - 450) + 'px', y = (e.clientY - 450) + 'px'; for (var i = 0; i < tooltips.length; i++) { tooltips[i].style.top = y; tooltips[i].style.left = x; } }; 
 div.img { margin: 5px; border: 1px solid #ccc; float: left; width: 240px; } div.img img { width: 240px; height:200px; } .container { background:white; padding: 20px; position: relative; margin: auto; border-radius: 5px; } .tooltipbox { width: 400px; min-height: 400px; background: white; opacity: .95; padding: 20px; } .tooltip { text-decoration:none; position:relative; } .tooltip span { display:none; } .tooltip:hover span { display:block; position:fixed; overflow:hidden; z-index: 9999; } 
 <div class="container"> <div class="img"> <a href="html/takraw.html" class="tooltip"> <img class="img" src="images/sepak-takraw.jpg" alt="img"> <span> <div class="tooltipbox"> <h2>Sepak Takraw</h2> </div> </span> </a> <div class="desc">Sepak Takraw</div> </div> <div class="img"> <a href="#" class="tooltip"> <img src="images/archery.jpg" alt="img"/> <span> <div class="tooltipbox"> <h2>Archery</h2> </div> </span> </a> <div class="desc">Archery</div> </div> </div> 

var tooltips = document.querySelectorAll('.tooltip span');

window.onmousemove = function (e) {
    var y = (e.clientY + 20) + 'px',
        width = document.body.clientWidth,
        toolTipWidth = 400;
    if (width - e.clientX > toolTipWidth) {
        x = (e.clientX + 20) + 'px';
    } else {
        x = (e.clientX - 40 - toolTipWidth) + 'px';
    }

    for (var i = 0; i < tooltips.length; i++) {
        tooltips[i].style.top = y;
        tooltips[i].style.left = x;
    }
};

Check the length between your mouse and the right edge of the browser.If the length is less than the ToolTip 's width, change your x value with conditional statement. 检查鼠标和浏览器右边缘之间的长度。如果长度小于ToolTip的宽度,请使用条件语句更改x值。

Since you have a specific width of the ToolBox ,I hard coded it into js file. 由于您具有特定宽度的ToolBox,因此我将其硬编码为js文件。

I'm not sure it is the best, but it works 我不确定这是否是最好的方法,但是它能起作用

我认为这可以解决您的问题:[工作代码] [1]

  [1]: https://jsfiddle.net/d0kwo0d0/1/

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

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