鼠标滑过某div出现另一被隐藏div的常见写法:display属性

display属性:
none 此元素不会被显示。
block 此元素将显示为块级元素,此元素前后会带有换行符。(较适用于ab不同时显示)
inline 默认。此元素会被显示为内联元素,元素前后没有换行符。(适用于ab同时显示)
inline-block 行内块元素。(CSS2.1 新增的值)

1、利用js函数,实现滑在b上,a由隐藏变显示

<!DOCTYPE html>
<div class='a' id='a' style="display:none">a is hidden</div><br/>
<div class='b' id='b'  onmouseover="mover();" onmouseout="mout();">b is show</div>

<script type="text/javascript">
function mover() {
	document.getElementById('a').style.display='inline'; 
}
function mout() {
	document.getElementById('a').style.display='none'; 
}
</script>

</html>

页面效果:
鼠标滑过前:
b is show
滑过时:
a is hidden
b is show

2、利用css样式,实现滑在b(实际是c)上,b变成a

<!DOCTYPE html>

<style type="text/css">
.a{display:none;}
.c:hover .a{display:block;}
.c:hover .b{display:none;}
</style>

<li class="c">
<div class='a' >a is hidden</div>
<div class='b'>b is show</div>
</li>
</html>

页面效果:
鼠标滑过前:b is show
滑过时:a is hidden

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 使用CSS3中的text-overflow属性 ```css div { height: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } ``` 2. 使用JavaScript计算高度并设置样式 ```html <div id="myDiv"> <p>...</p> <p>...</p> <p>...</p> ... </div> ``` ```css #myDiv { height: 100px; overflow: hidden; } ``` ```javascript const div = document.getElementById('myDiv'); const p = div.getElementsByTagName('p'); let totalHeight = 0; for (let i = 0; i < p.length; i++) { totalHeight += p[i].offsetHeight; } if (totalHeight > div.offsetHeight) { div.style.height = div.offsetHeight + 'px'; div.style.overflow = 'hidden'; div.style.textOverflow = 'ellipsis'; div.style.whiteSpace = 'nowrap'; p[p.length - 1].style.display = 'none'; const span = document.createElement('span'); span.innerHTML = '...'; div.appendChild(span); } ``` 3. 使用jQuery计算高度并设置样式 ```html <div id="myDiv"> <p>...</p> <p>...</p> <p>...</p> ... </div> ``` ```css #myDiv { height: 100px; overflow: hidden; } ``` ```javascript const div = $('#myDiv'); const p = div.find('p'); let totalHeight = 0; p.each(function() { totalHeight += $(this).outerHeight(true); }); if (totalHeight > div.height()) { div.css({ height: div.height(), overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }); p.last().hide(); const span = $('<span>...</span>'); div.append(span); } ``` 4. 使用Vue.js计算高度并设置样式 ```vue <template> <div ref="myDiv"> <p>...</p> <p>...</p> <p>...</p> ... </div> </template> <script> export default { mounted() { const div = this.$refs.myDiv; const p = div.getElementsByTagName('p'); let totalHeight = 0; for (let i = 0; i < p.length; i++) { totalHeight += p[i].offsetHeight; } if (totalHeight > div.offsetHeight) { div.style.height = div.offsetHeight + 'px'; div.style.overflow = 'hidden'; div.style.textOverflow = 'ellipsis'; div.style.whiteSpace = 'nowrap'; p[p.length - 1].style.display = 'none'; const span = document.createElement('span'); span.innerHTML = '...'; div.appendChild(span); } } } </script> ``` 5. 使用React计算高度并设置样式 ```jsx import React, { useEffect, useRef } from 'react'; export default function MyComponent() { const divRef = useRef(null); useEffect(() => { const div = divRef.current; const p = div.getElementsByTagName('p'); let totalHeight = 0; for (let i = 0; i < p.length; i++) { totalHeight += p[i].offsetHeight; } if (totalHeight > div.offsetHeight) { div.style.height = div.offsetHeight + 'px'; div.style.overflow = 'hidden'; div.style.textOverflow = 'ellipsis'; div.style.whiteSpace = 'nowrap'; p[p.length - 1].style.display = 'none'; const span = document.createElement('span'); span.innerHTML = '...'; div.appendChild(span); } }, []); return ( <div ref={divRef}> <p>...</p> <p>...</p> <p>...</p> ... </div> ); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值