使 CSS 悬停停留在视口内
我有一个使用 CSS 设置的悬停解决方案。但是,悬停图像不尊重视口,因此最终显示在视口之外。我计划简单地创建新的类,根据设计中图像的位置指定偏移量,但由于我无法控制用户使用的分辨率,所以我认为应该有某种方法来强制悬停在视口。有谁知道我该如何做到这一点?
我有以下 CSS:
.thumbnail:hover {
background-color: transparent;
z-index: 50;
}
.thumbnail span {
position: absolute;
padding: 5px;
left: -1000px;
border: 1px dashed gray;
visibility: hidden;
color: black;
text-decoration: none;
}
.thumbnail span img {
border-width: 0;
padding: 2px;
}
.thumbnail:hover span {
visibility: visible;
top: 0;
left: 70px;
}
要将以下缩略图与悬停匹配:
<li>
<a href="http://www.yahoo.com" class="img thumbnail">
<img src="1_s.jpg" />
<span><img src="1_b.jpg" /></span>
</a>
</li>
<li>
<a href="http://www.google.com" class="img thumbnail">
<img src="2_s.jpg" />
<span><img src="2_b.jpg" /></span>
</a>
</li>
我这里有一个示例页面显示行为:
http ://estorkdelivery.com/example/example2.html
将鼠标悬停在底部的图像上可查看悬停图像显示在视口之外。
谢谢!
更新 2/22/2012 我测试了下面的答案#1,但它引入了新的问题,例如需要更改透明度以及需要让悬停图像始终从图像的左上角显示- 我发现这两个问题都无法使用脚本选项进行修改。有人有其他建议或修改答案#1中脚本的方法吗?另外,我应该添加我正在寻找的内容,因为更多的最终结果是 istockphoto.com 上图像的悬停样式,其中图像始终出现在它们悬停的图像左侧或右侧的同一位置,而不是基于您将鼠标悬停在图像上时鼠标的位置。
I have a hover solution setup with CSS. However, the hover images don't respect the viewport and therefore end up displaying outside of it. I planned to simply create new classes specifying the offset depending on the location of the image within my design, but since I can't control the resolution the user is using, I was thinking there should be some way to force the hover to display within the viewport. Does anyone have an idea on how I can do this?
I have the following CSS:
.thumbnail:hover {
background-color: transparent;
z-index: 50;
}
.thumbnail span {
position: absolute;
padding: 5px;
left: -1000px;
border: 1px dashed gray;
visibility: hidden;
color: black;
text-decoration: none;
}
.thumbnail span img {
border-width: 0;
padding: 2px;
}
.thumbnail:hover span {
visibility: visible;
top: 0;
left: 70px;
}
To match the following thumbnails with hover:
<li>
<a href="http://www.yahoo.com" class="img thumbnail">
<img src="1_s.jpg" />
<span><img src="1_b.jpg" /></span>
</a>
</li>
<li>
<a href="http://www.google.com" class="img thumbnail">
<img src="2_s.jpg" />
<span><img src="2_b.jpg" /></span>
</a>
</li>
I have a sample page here displaying the behavior:
http://estorkdelivery.com/example/example2.html
Hover over the images at the bottom to see the hover image display outside of the viewport.
Thanks!
Update 2/22/2012 I tested answer #1 below, but it introduced new issues such as the need to change the transparency and the need to have the hover image always display from the top left of the image - both issues I saw no way of modifying with the script options. Anyone have other suggestions or a way to modify the script in answer #1? Also, I should add what I'm looking for as more of the final result is the hover styling of images on istockphoto.com where the images always appear in the same spot to the left or right of the images they are hovering over and not based off the position of the mouse as you hover over the image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我为您创建了一个定制插件!
http://jsfiddle.net/adaz/tAECz/
嗯,这很基本,但我认为它满足你的标准。您可以通过两种方式激活它:
1)如果您不愿意为每个图像创建缩略图,您可以简单地像这样列出您的图像:
2)如果您确实想创建自己的缩略图,您的 html 应该如下所示 无论您选择哪种
方式,您都需要在页面中包含 jQuery 1.7+ 以及我的插件。
您需要做的最后一件事是激活它,如果您要选择第一个选项,您可以在页面中包含以下代码:
如果您要选择第二个选项,您需要覆盖默认设置,例如这:
这更像是一个原型,因此它的功能非常有限,但您可以设置一些选项,例如:
拇指最大宽度:100
拇指最大高度:100
工具提示宽度:200
工具提示高度:150
transitionSpeed: 100
如果您喜欢它,我很乐意花一些时间在上面并调整它以满足您的需求!
I've created a bespoke plugin for you!
http://jsfiddle.net/adaz/tAECz/
Well, it's pretty basic but I think it meets your criteria. You can activate it in two ways:
1) If you can't be bothered creating thumbnails for every image, you can just simply list your images like this:
2) If you really want to create your own thumbnails, your html should look like this:
Either way you choose, you need to include jQuery 1.7+ in your page along with my plugin.
The very last thing you need to do is to activate it, if you're going for the first option, you can just include in your page following code:
If you're going for the second option, you need to override default settings like this:
This is more like a prototype so it's quite limited in terms of functionaltiy but you can set some options like:
thumbMaxWidth: 100
thumbMaxHeight: 100
tooltipWidth: 200
tooltipHeight: 150
transitionSpeed: 100
If you like it, I'm happy to spend some time on it and adjust it to suit your needs!
尝试 jQuery 工具提示:
这里是根据您的要求提供的示例:
http://jsfiddle.net/cadence96/3X2eZ/
文档
http://docs.jquery.com/Plugins/Tooltip
http://jquery.bassistance.de/tooltip/demo/
快速说明:
1) 在内加载 css 和脚本:
2) 仍在 内放置执行脚本:
类“.my-div”将用于通过悬停事件显示图像。
“.my-div”的同级 div 必须包含隐藏元素,以便在悬停后可见。
仅此而已!
Try the jQuery Tooltip:
Here is an example according to your request:
http://jsfiddle.net/cadence96/3X2eZ/
DOCS
http://docs.jquery.com/Plugins/Tooltip
http://jquery.bassistance.de/tooltip/demo/
Quick instructions:
1) Within the <head> load the css and scripts:
2) Still within the <head> place the execution script:
The class '.my-div' will be used to display the image with the hover event.
The sibling div to '.my-div' must contain the hidden elements to make visible after hovering.
That's all!
我开发了一个脚本,在js中你可以改变容器的宽度。
这是小提琴:
http://jsfiddle.net/cadence96/GgDqh/
已更新,现在可以在 FDDLE 中使用并且适应性更强。
I've developed a script, in the js you can change the width of the container.
Here is the fiddle:
http://jsfiddle.net/cadence96/GgDqh/
UPDATED, NOW WORKS IN FIDDLE AND IS MORE ADAPTABLE.
更新:
查看源代码
注意:这没有考虑弹出窗口大于窗口的情况,在这在这种情况下,您不仅应该更改偏移位置,还必须调整弹出窗口的大小以适应窗口。
UPDATED:
look at the source code
NB: this does not take in consideration cases in which the popup is bigger then the window, in this case you should not only change the offset position but you have to resize the popup to fit the window.
这有效。然而我只解决了 Y 轴的问题。对于水平偏移,它应该是相同的。 在此处查看其工作原理。
我将
top
设置为图像的一侧 * -1,如果图像不合适,这会将图像对齐到底部,将该值更改为您想要的值。图像非常大,但是这个脚本并不是万无一失的。您必须获得整个可见区域,以确保在重新定位它时不会在另一侧切掉它。标记没有改变,我只是添加了 Jquery:
希望它有帮助!
This works. I however solved the problem only for the Y-axis. For the horizontal offset it should be the same. See how it works here.
I set the
top
to exactly the side of the image * -1, this aligns the image at the bottom if it doesn't fit, change that value to whatever you want. The images are very big and this script is however not gonna be bulletproof. You would have to get the whole visible area to make sure it doesn't cut out on the other side when you re-position it.The markup hasn't changed, I just added the Jquery:
Hope it helped!!
试试这个……看起来比较酷!
Try this one... seems comparatively cool!