简体   繁体   English

仅用JS悬停时,如何在网站上显示元素?

[英]How can I show a element on my site when hovered with only JS?

EDIT: 编辑:

Hi, I would like to know how could I show a little X or remove icon when a user hovers over the inbox 嗨,我想知道当用户将鼠标悬停在收件箱上时如何显示X或删除图标

I tried to hide the remove icon first and then add this effect to a <p> element :hover {display:block;} ,I don't know what I did wrong but it didn't work 我尝试先隐藏删除图标,然后将此效果添加到<p>元素中:hover {display:block;} ,我不知道我做错了什么,但是没有用

http://jsbin.com/zijewohoru/1/edit?html,css,output http://jsbin.com/zijewohoru/1/edit?html,css,输出

Is there anyway I could do it with JS or would it be a lot easier to do it with CSS? 无论如何,我可以用JS做到这一点,还是用CSS容易得多?

I think something like this is what you are looking for. 我认为您正在寻找类似这样的东西。

/* Hide icon by default */
.box i {
  display: none;
}

/* Show icon on containing element hover */
.box:hover i {
  display: block;
}

For a fade-in transition, this would work. 对于淡入过渡,这将起作用。

.box i {
  opacity: 0;
  -webkit-transition: opacity .3s ease-in;
  -moz-transition: opacity .3s ease-in;
  -o-transition: opacity .3s ease-in;
  -ms-transition: opacity .3s ease-in;
  transition: opacity .3s ease-in;
}

.box:hover i {
  opacity: 1;
}

Add this 加上这个

 .box:hover > i { display: block; } .box > i { display: none; } 

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

相关问题 仅当元素在AngularJS中悬停时才如何分配变量? - How do I assign a variable only when an element is hovered in AngularJS? 仅当标题悬停在上方时,如何设置菜单才能滑出? - How can I set up my menu to slide out, only when the header is hovered on? 使用angularjs,如何仅当值不在数组中时才显示元素? - With angularjs how can I show an element only when a value is not in an array? 有什么方法我只能在工具提示(Recharts)中显示悬停线? - Is there any way that I can only show the hovered line in tooltip (Recharts)? 如何在循环元素和使用 addEventListener 时仅更改悬停元素的样式? - How can I change only the hovered element's styling while looping through elements and using addEventListener? 如何在具有特定类的元素悬停时调用js函数? - How to call a js function when an element with a particular class is hovered? 悬停时如何获取VSCode以显示我的库函数参数 - How to get VSCode to show my libraries function arguments when hovered 我怎样才能让我的 Etch-A-Sketch 网格在悬停时改变颜色和不透明度? - How can i make my Etch-A-Sketch grid change color and opacity when hovered? 悬停时如何让我的 img src 在 3-4 张图像之间不断变化? - How can I get my img src to keep changing between 3-4 images when hovered? 如何使用 react 获取正在悬停的元素? - How can I get what element is being hovered using react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM