How to check if marquee is scrolling

浪子不回头ぞ 提交于 2021-01-28 04:37:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!