I want to apply the hover state remotely, by hovering on a different object. But I want to name the object that has its hover activated rather than have it be by DOM relationship to the item being hovered over.
我想要远程应用悬停状态,通过悬停在另一个对象上。但是,我想要命名具有悬停激活的对象,而不是通过DOM关系将其命名为悬停的对象。
<style>
img:hover {border: thin red solid;}
</style>
<li id="hover-over-me">Dogs</li>
<img src="dog.jpg" />
I haven't found a javascript or jquery method that allows you to apply the hover pseudo-class effect to an element remotely (ie independently of it actually being hovered). Is there a way to do this?
我还没有找到一种javascript或jquery方法允许您远程地将悬浮伪类效果应用到一个元素(即独立于它实际被悬浮)。有办法吗?
2 个解决方案
#1
1
If you mean specifically the CSS :hover
pseudo selector, then one element can only trigger it on another insofar as a relationship can be established in CSS:
如果您是指CSS: hover pseudo selector,那么一个元素只能在CSS中建立关系时在另一个元素上触发:
http://jsfiddle.net/tFSWt/
example as siblings:
例子的兄弟姐妹们:
<span>Sibling </span><img src = "http://dummyimage.com/120x90/f00/fff.png&text=my+image" />
img:hover { border: 2px dashed blue; }
span:hover + img { border: 2px dashed blue; }
example as ancestor/descendant:
示例祖先/后代:
<span>Parent
<img src = "http://dummyimage.com/120x90/f00/fff.png&text=my+image" />
</span>
img:hover { border: 2px dashed blue; }
span:hover img { border: 2px dashed blue; }
Otherwise you'll need to rely on JavaScript to select the related element and set the style on either by inline styling, or by adding a class that provides the appropriate style.
否则,您将需要依赖JavaScript来选择相关元素并通过内联样式或添加提供适当样式的类来设置样式。
#2
3
http://sandbox.phpcode.eu/g/3304b
http://sandbox.phpcode.eu/g/3304b
<style>
img:hover,img.hovered {border: 5px red solid;}
</style>
<ul>
<li id="hover-over-me">Dogs</li>
</ul>
<img src="http://4.bp.blogspot.com/_7VyEDKFMA2U/TOX9ZL7KRPI/AAAAAAAAACM/-XSoYjePBPk/s1600/cute-puppy-dog-wallpapers.jpg" />
<script>
$("li").mouseenter(function(){
$("img").addClass('hovered');
});
$("li").mouseout(function(){
$("img").removeClass('hovered');
});
</script>
#1
1
If you mean specifically the CSS :hover
pseudo selector, then one element can only trigger it on another insofar as a relationship can be established in CSS:
如果您是指CSS: hover pseudo selector,那么一个元素只能在CSS中建立关系时在另一个元素上触发:
http://jsfiddle.net/tFSWt/
example as siblings:
例子的兄弟姐妹们:
<span>Sibling </span><img src = "http://dummyimage.com/120x90/f00/fff.png&text=my+image" />
img:hover { border: 2px dashed blue; }
span:hover + img { border: 2px dashed blue; }
example as ancestor/descendant:
示例祖先/后代:
<span>Parent
<img src = "http://dummyimage.com/120x90/f00/fff.png&text=my+image" />
</span>
img:hover { border: 2px dashed blue; }
span:hover img { border: 2px dashed blue; }
Otherwise you'll need to rely on JavaScript to select the related element and set the style on either by inline styling, or by adding a class that provides the appropriate style.
否则,您将需要依赖JavaScript来选择相关元素并通过内联样式或添加提供适当样式的类来设置样式。
#2
3
http://sandbox.phpcode.eu/g/3304b
http://sandbox.phpcode.eu/g/3304b
<style>
img:hover,img.hovered {border: 5px red solid;}
</style>
<ul>
<li id="hover-over-me">Dogs</li>
</ul>
<img src="http://4.bp.blogspot.com/_7VyEDKFMA2U/TOX9ZL7KRPI/AAAAAAAAACM/-XSoYjePBPk/s1600/cute-puppy-dog-wallpapers.jpg" />
<script>
$("li").mouseenter(function(){
$("img").addClass('hovered');
});
$("li").mouseout(function(){
$("img").removeClass('hovered');
});
</script>