问题描述
我试图在 div 悬停时影响外部元素.像这样的:
<div class="affected">你好</div><div class="hover-me"></div></div>CSS:
.hover-me:hover ~ .affected {颜色:}
我尝试过使用其他同级选择器,但它不起作用.
解决方案 使用纯 CSS 会变得非常棘手.
一种方法,如果您不需要包含可悬停子项的 div 上的指针事件(悬停、点击等),将容器设置为可操作 div,禁用指针事件,在孩子上重置它们,并使用某种魔法使兄弟姐妹在您的 HTML 上以相反的顺序排列,这样它们就可以用兄弟姐妹选择器作为目标(因为您不能定位以前的兄弟姐妹)
有点像
body{/*切换 .affected 和 .hover-container 的顺序,因此 .affected 可以在 HTML 的下方,并以兄弟选择器为目标,同时显示在上面*/显示:弹性;弹性方向:列反向;}.hover-container:hover ~ .affected{/*针对容器上的操作*/背景:红色;}.悬停容器{/*禁用容器上的指针事件,因此进入它不会对兄弟造成悬停效果*/指针事件:无;}.hover-我{/*重置 .hover-me 子对象上的指针事件,因此我们为容器设置的先前 :hover 仅在悬停该子对象时才有效*/指针事件:自动;光标:指针;}分区{边框:2px 纯灰色;边距:20px 40px;文本对齐:居中;}
<div class="hover-container">这是悬停容器<div class="hover-me">悬停我</div></div><div class="受影响">做作的</div>
但这可能是一种不太常见的情况,到那时你最好使用 JS 方法.
I'm trying to affect an outside element when a div is hovered. Something like this:
<div class="affected">
Hi
</div>
<div>
<div class="hover-me"></div>
</div>
CSS:
.hover-me:hover ~ .affected {
color:
}
I've tried with other sibling selectors but it doesn't work.
解决方案 With pure CSS that's gonna be as tricky as it gets.
An approach, IF you don't need pointer-events (hover, clicks, etc) on the div that contains the hoverable child, is setting the container as actionable div, disabling pointer-events, resetting them on the child, and using some sort of magic to have the siblings in reverse order on your HTML so they can be targeted with sibling selectors (as you cannot target previous siblings)
Something like
body{
/*switches the oder of .affected and .hover-container, so .affected can be bellow in the HTML and be targeted with sibling selectors, while showing above*/
display:flex;
flex-direction:column-reverse;
}
.hover-container:hover ~ .affected{
/*targets the action on the container*/
background:red;
}
.hover-container{
/*disables pointer-events on the container, so entering it won't cause the hover effect on the sibling*/
pointer-events:none;
}
.hover-me{
/*resets pointer-events on the .hover-me child, so the previous :hover we set for the container will work only when hovering this child*/
pointer-events:auto;
cursor:pointer;
}
div{
border:2px solid grey;
margin:20px 40px;
text-align:center;
}
<div class="hover-container">
this is the hover container
<div class="hover-me">hover me</div>
</div>
<div class="affected">
affected
</div>
But that's probably a not so common scenario, at that point you'll be better off with a JS approach.
这篇关于悬停在 div 上会影响外部元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09
前端开发问题
313
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26
前端开发问题
493
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24
前端开发问题
271
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20
前端开发问题
13
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20
前端开发问题
6
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20
前端开发问题
6
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)