在Firefox中
mouseover
和
mouseout
多次开火。事实上,很多事情都会导致它们重新燃烧。在这里,更改占位符属性可以做到这一点,可能是因为FX在更改视觉内容后正在重新计算布局。其他浏览器中可能存在您尚未发现的类似问题。
你不能依赖
鼠标悬停
和
灭鼠器
(或任何姿势事件)只需开火一次即可开始效果,并再次停止效果。
相反,将其设为输入状态并切换标志:
$(function() {
var sppInput,
sppInputName = $('#spp-input-name'),
sppInputNamePlace = sppInputName.attr('placeholder'),
inputAnimating = false;
function sppInputStart(elm, n, text) {
if(!inputAnimating) {
setTimeout(function () {
sppInputStart(elm, n, text);
}, 500);
return;
}
if (n < (text.length)) {
$(elm).attr('placeholder', text.substring(0, n + 1));
n++;
sppInput = setTimeout(function () {
sppInputStart(elm, n, text);
}, 80);
}
}
function sppInputStop(elm, place) {
clearTimeout(sppInput);
$(elm).attr('placeholder', place);
}
sppInputName.mouseover(function () {
inputAnimating = true;
});
sppInputName.mouseout(function () {
inputAnimating = false;
});
sppInputStart(sppInputName, 0, sppInputName.data('typewriter'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder="Name" data-typewriter="Insert the Name" type="text" id="spp-input-name" name="name" required>
这在FX中有效。
但是,请考虑使用
requestAnimationFrame
或者使用
placeholder
属性,因为这可能会导致帧丢失,尤其是在移动设备上。
您还可能会导致各种问题,因为屏幕阅读器在读取您正在更改的属性时会遇到问题,每次更改时间为80ms。