javascript - 图像因 onmouseover 事件而抖动
问题描述
我无法解决鼠标悬停时抖动图像的问题。我有以下脚本:
function displayText(id)
{
var el = document.getElementById(id);
el.style.display = 'block';
}
function hideText(id)
{
var el = document.getElementById(id);
el.style.display = 'none';
}
.btn {
margin-top: -90px;
z-index: 999999999;
position: absolute;
text-align: center;
background: #37a000;
displa: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href='#' >
<img class="streamer_img" alt="example" src="https://placeimg.com/640/480/nature"
onmouseover="this.src='https://placeimg.com/640/480/people'; displayText('hiddentext')"
onmouseout="this.src='https://placeimg.com/640/480/nature'; hideText('hiddentext')" />
<p class="btn" id="hiddentext" style="display: none;">Watch/play</p>
</a>
一切都很好,但是当我将鼠标移到显示的 Watch 文本上时,一切都开始抖动。
我该如何解决这个麻烦?谢谢
解决方案
问题在于,当您将鼠标悬停在Watch
元素上时,它会触发更改图像的mouseout
事件。img
这反过来隐藏了文本,触发了mouseover
显示图像的文本......等等无限。
要解决此问题,您可以将事件处理程序附加到父a
元素并img
在其中找到要修改的元素,如下所示:
function displayText(id) {
var el = document.getElementById(id);
el.style.display = 'block';
}
function hideText(id) {
var el = document.getElementById(id);
el.style.display = 'none';
}
.btn {
margin-top: -90px;
z-index: 999999999;
position: absolute;
text-align: center;
background: #37a000;
displa: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href='#' onmouseenter="this.getElementsByClassName('streamer_img')[0].src='https://placeimg.com/640/480/people'; displayText('hiddentext')" onmouseleave="this.getElementsByClassName('streamer_img')[0].src='https://placeimg.com/640/480/nature'; hideText('hiddentext')">
<img class="streamer_img" alt="example" src="https://placeimg.com/640/480/nature" />
<p class="btn" id="hiddentext" style="display: none;">Watch/play</p>
</a>
请注意,我将事件处理程序更改为onmouseenter
并onmouseleave
获得更好的性能。
但是您应该注意,使用on*
事件属性已经非常过时了。您应该改用不显眼的事件处理程序,如下所示:
window.addEventListener('load', function() {
var a = document.querySelector('a');
var img = document.querySelector('img');
var p = document.querySelector('p');
a.addEventListener('mouseenter', function() {
img.src = a.dataset.hoversrc;
p.style.display = 'block';
});
a.addEventListener('mouseleave', function() {
img.src = a.dataset.leavesrc;
p.style.display = 'none';
});
});
.btn {
margin-top: -90px;
z-index: 999999999;
position: absolute;
text-align: center;
background: #37a000;
displa: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href='#' data-hoversrc="https://placeimg.com/640/480/people" data-leavesrc="https://placeimg.com/640/480/nature">
<img class="streamer_img" alt="example" src="https://placeimg.com/640/480/nature" />
<p class="btn" id="hiddentext" style="display: none;">Watch/play</p>
</a>
推荐阅读
- javascript - 如何使用javascript在计算器中添加小数?
- javascript - 在 React 组件之外使用 NextRouter
- javascript - 尝试使用 mongodb 和 nodejs 登录用户
- html - Bootstrap - 在导航栏中定位
- r - 如何修复R中for循环中的错误“提供的元素多于替换的元素”?
- python-3.x - pandas._libs.hashtable.PyObjectHashTable.get_item KeyError:
- powershell - 用文件名替换特定行号的文本
- python - 是否有一种内置方法可以从 TensorFlow/Keras 中的文件加载数据增强配置?
- javascript - 如何从 cypress 测试中删除“_fs.readFileSync 不是函数”错误
- python-3.x - 有没有办法根据轴长度来缩放 pyplot.scatter 图的标记?