如何使用jQuery检测字幕是否在按钮点击时滚动?
<marquee>Scrolling text here, moving constantly</marquee>
<button id="btnCheck">Button</button>
我的jQuery代码如下所示:
$(function (){
var marquee = $("marquee")[0];
$("#btnCheck").click(function (){
// How to check if marquee is scrolling here
});
});发布于 2017-10-11 08:30:58
您可以在marquee中添加p并检查其left位置。如果更改了,则marquee正在滚动,否则不会滚动,如下面的代码片段所示:
$(function() {
$("#btnCheck").click(function() {
var initialLeft = $('marquee > p').position().left;
setTimeout(function() {
var currentLeft = $('marquee > p').position().left;
if (initialLeft !== currentLeft) {
alert("Marquee is scrolling!");
}else{
alert("Marquee is not scrolling");
}
}, 100);
});
});p {
margin: 0
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<marquee loop="1"><p>Scrolling text here, moving constantly</p></marquee>
<button id="btnCheck">Button</button>
P/s:
这个(超文本标记语言
<marquee>)特性已经过时了。虽然它仍然可以在一些浏览器中工作,但不鼓励使用它,因为它可以随时删除。尽量避免使用它。
https://stackoverflow.com/questions/46677780
复制相似问题