问题
How to detect if marquee is scrolling on button click using jQuery?
<marquee>Scrolling text here, moving constantly</marquee>
<button id="btnCheck">Button</button>
My jQuery code looks like:
$(function (){
var marquee = $("marquee")[0];
$("#btnCheck").click(function (){
// How to check if marquee is scrolling here
});
});
回答1:
You can add a p
inside the marquee
and check its left
position. If it is changed, then your marquee
is scrolling, otherwise, it is not, like this snippet:
$(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:
This (The HTML
<marquee>
) feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
Refer: developer.mozilla.org
来源:https://stackoverflow.com/questions/46677780/how-to-check-if-marquee-is-scrolling