Jquery 多悬停高光
我有一个想法,一种给用户反馈的方法 我喜欢将鼠标悬停在菜单上,它将突出显示与菜单对应的img 反之,将鼠标悬停在图像上将突出显示菜单 我认为它可以用jquery来完成,但是它可以用纯css来完成吗?或者你有没有一个例子或代码可以让我的想法建立在上面Jquery 多悬停高光,jquery,css,hover,Jquery,Css,Hover,我有一个想法,一种给用户反馈的方法 我喜欢将鼠标悬停在菜单上,它将突出显示与菜单对应的img 反之,将鼠标悬停在图像上将突出显示菜单 我认为它可以用jquery来完成,但是它可以用纯css来完成吗?或者你有没有一个例子或代码可以让我的想法建立在上面 谢谢只要要突出显示的图像是您悬停的项目的兄弟或后代,就可以相对轻松地完成 兄弟姐妹 我很好奇你为什么要采用这种方法,似乎你想把鼠标悬停在其中一个项目上(在本例中是“缩略图”和“全尺寸”)来显示另一个项目?这意味着这些项目中的一个或两个将同时不可见/隐
谢谢只要要突出显示的图像是您悬停的项目的兄弟或后代,就可以相对轻松地完成 兄弟姐妹 我很好奇你为什么要采用这种方法,似乎你想把鼠标悬停在其中一个项目上(在本例中是“缩略图”和“全尺寸”)来显示另一个项目?这意味着这些项目中的一个或两个将同时不可见/隐藏。在这种情况下,用户如何知道他们的存在是为了进行交互 演示(基于兄弟姐妹)
这里有一个替代方案,用于在两者之间建立“页面上的任意位置”关系: 演示:在。 html:
与',有点不同,它不需要明确的关系,但它确实要求
缩略图的顺序与对应的全尺寸元素的顺序相同(反之亦然)因为它基于它们的索引位置。所以在jQuery中,据我所知,您有一个菜单和一些图像:
<ul>
<li><a href="#">item 1</a></li>
<li><a href="#">item 2</a></li>
</ul>
...
<img src="/img1.jpg" />
<img src="/img2.jpg" />
您可以使用.text()或.atrr('src')将任何东西真正用作标识符
highlight类将包含滚动的样式,对于菜单和图像,无论是使用li.hightlight和img.highlight,还是使用不同的类,这些样式都可能明显不同。假设您可以在图像上使用绝对定位,则无需Javascript即可完成此操作。只需将img
嵌套在元素内部(可能是一个锚点,否则在IE6中不起作用),将其绝对定位(在各种菜单项上使用ID以不同方式定位图像),并向锚点添加悬停样式。悬停动作适用于绝对定位的img
和包含a
元素
下面是一个非常简单的例子:
<!DOCTYPE html>
<html>
<head>
<style>
#container {
margin: 10px auto;
padding: 0;
position: relative;
width: 960px;
}
#menu {
list-style: none;
margin: 0px;
padding: 100px 0px; /* simulate header area */
}
#menu a {
border: 1px solid #fff;
float: left;
}
#menu a:hover,
#menu a:hover img {
border: 1px solid #f00;
}
#menu img {
border: 1px solid #fff;
position: absolute;
top: 0px;
}
#test1 img {
left: 0px;
}
#test2 img {
left: 40px;
}
.clear {
clear: left;
}
</style>
</head>
<body>
<div id="container">
<ul id="menu">
<li><a href="#" id="test1">here is some menu text <img src="http://www.gravatar.com/avatar/9565672b231ef0e90d5a625699f2eafc?s=32&d=identicon&r=PG" /></a></li>
<li><a href="#" id="test2">here is some more menu text <img src="http://www.gravatar.com/avatar/9565672b231ef0e90d5a625699f2eafc?s=32&d=identicon&r=PG" /></a></li>
</ul>
<div class="clear"></div>
</div>
</body>
</html>
#容器{
利润率:10px自动;
填充:0;
位置:相对位置;
宽度:960px;
}
#菜单{
列表样式:无;
边际:0px;
填充:100px 0px;/*模拟收割台区域*/
}
#菜单a{
边框:1px实心#fff;
浮动:左;
}
#菜单a:悬停,
#菜单a:悬停img{
边框:1px实心#f00;
}
#菜单img{
边框:1px实心#fff;
位置:绝对位置;
顶部:0px;
}
#测试1 img{
左:0px;
}
#测试2 img{
左:40px;
}
.清楚{
清除:左;
}
$(document).ready(
function(){
$('.fullsize.').hover(
function() {
$(this).closest('.thumbnail').show();
}
);
}
);
$(document).ready(
function(){
$('.fullsize, .thumbnail').mouseover(
function() {
var currId = $('.fullsize').index();
if ($(this).hasClass('thumbnail')) {
var toShow = '.fullsize';
}
else {
var toShow = '.thumbnail';
}
$(toShow).eq(currId).addClass('show');
}
);
$('.fullsize, .thumbnail').mouseout(
function() {
var currId = $('.fullsize').index();
if ($(this).hasClass('thumbnail')) {
var toShow = '.fullsize';
}
else {
var toShow = '.thumbnail';
}
$(toShow).eq(currId).removeClass('show');
}
);
}
);
<ul>
<li><a href="#">item 1</a></li>
<li><a href="#">item 2</a></li>
</ul>
...
<img src="/img1.jpg" />
<img src="/img2.jpg" />
<ul>
<li><a href="#" rel="img1">item 1</a></li>
<li><a href="#" rel="img2">item 2</a></li>
</ul>
...
<img src="/img1.jpg" id="img1" />
<img src="/img2.jpg" id="img2" />
$(function(){
$('li a').bind('mouseenter mouseleave',function(e){
$(this).toggleClass('highlight');
$('#'+$(this).attr('rel')).toggleClass('highlight');
});
$('img[id^=img]').bind('mouseenter mouseleave',function(e){
$(this).toggleClass('highlight');
$('a[rel='+$(this).attr('id')+']').toggleClass('highlight');
});
});
<!DOCTYPE html>
<html>
<head>
<style>
#container {
margin: 10px auto;
padding: 0;
position: relative;
width: 960px;
}
#menu {
list-style: none;
margin: 0px;
padding: 100px 0px; /* simulate header area */
}
#menu a {
border: 1px solid #fff;
float: left;
}
#menu a:hover,
#menu a:hover img {
border: 1px solid #f00;
}
#menu img {
border: 1px solid #fff;
position: absolute;
top: 0px;
}
#test1 img {
left: 0px;
}
#test2 img {
left: 40px;
}
.clear {
clear: left;
}
</style>
</head>
<body>
<div id="container">
<ul id="menu">
<li><a href="#" id="test1">here is some menu text <img src="http://www.gravatar.com/avatar/9565672b231ef0e90d5a625699f2eafc?s=32&d=identicon&r=PG" /></a></li>
<li><a href="#" id="test2">here is some more menu text <img src="http://www.gravatar.com/avatar/9565672b231ef0e90d5a625699f2eafc?s=32&d=identicon&r=PG" /></a></li>
</ul>
<div class="clear"></div>
</div>
</body>
</html>