Javascript 如何在鼠标悬停时暂停新闻滑块,并在鼠标退出时播放。?

Javascript 如何在鼠标悬停时暂停新闻滑块,并在鼠标退出时播放。?,javascript,html,Javascript,Html,创新——4S的力量汽车工业的进步建立在四大基本支柱上 这一组合对汽车工业构成了威胁,这一说法并不夸张 创新——4S的力量汽车工业的进步建立在四大基本支柱上 这一组合对汽车工业构成了威胁,这一说法并不夸张 创新——4S的力量汽车工业的进步建立在四大基本支柱上 这一组合对汽车工业构成了威胁,这一说法并不夸张 函数tick(){ $('ticker li:first').slideUp(函数(){$(this).appendTo($('ticker')).slideDown();}); }

创新——4S的力量汽车工业的进步建立在四大基本支柱上

这一组合对汽车工业构成了威胁,这一说法并不夸张

  • 创新——4S的力量汽车工业的进步建立在四大基本支柱上

    这一组合对汽车工业构成了威胁,这一说法并不夸张

  • 创新——4S的力量汽车工业的进步建立在四大基本支柱上

    这一组合对汽车工业构成了威胁,这一说法并不夸张

  • 函数tick(){ $('ticker li:first').slideUp(函数(){$(this).appendTo($('ticker')).slideDown();}); } setInterval(function(){tick()},2000);

    这是简单新闻滑块的代码。
    如何在鼠标悬停时暂停新闻滑块并在鼠标悬停时播放。?

    将setInterval指定给一个变量,这样就只有一个interval对象可以重用

    var interval\u id=setInterval(

    在悬停事件中,使用clearInterval(interval_id)停止悬停,并在鼠标离开/退出时恢复悬停

    试试这个:

    其基本思想是清除悬停时的
    setInterval
    ,该选项将停止滑块,然后在鼠标移出时重新初始化滑块

    <body>
    <ul id="ticker">
        <li>
            <p>Innovation - The Power of 4S The progress of Auto Industry stands on the four basic pillars.</p>
            <p>The S pillars and it would not be an overstatement that the combination thr illed the Auto-Indutry.</p>
        </li>
        <li>
            <p>Innovation - The Power of 4S The progress of Auto Industry stands on the four basic pillars.</p>
            <p>The S pillars and it would not be an overstatement that the combination thr illed the Auto-Indutry.</p>
    
        </li>
        <li>
            <p>Innovation - The Power of 4S The progress of Auto Industry stands on the four basic pillars.</p>
            <p>The S pillars and it would not be an overstatement that the combination thr illed the Auto-Indutry.</p>
        </li>
    </ul>
    
    
    <script>
    function tick(){
        $('#ticker li:first').slideUp( function () { $(this).appendTo($('#ticker')).slideDown(); });
    }
    setInterval(function(){ tick () }, 2000);
    

    基本上在鼠标结束时停止调用该函数,并在鼠标离开时继续

    未标记jQuery,但您正在使用jQuery

    您可以使用来知道何时悬停元素,以及启动/停止滑块

    代码:

    演示:

    $(document).ready(function() {
    
         var clearTick = setInterval(function(){ tick () }, 2000);         
    
         $('div').hover(
             function () {
               clearInterval(clearTick);
    
             }, 
             function () {
               clearTick = setInterval(function(){ tick () }, 2000);
    
             }
         );
    
       });
    
    sliderTick = setInterval(function(){ tick () }, 2000);
    
    $('#ticker li').mouseover(function(){
         clearInterval(sliderTick);
    }
    
    $('#ticker li').mouseout(function(){
         sliderTick = setInterval(function(){ tick () }, 2000);
    }
    
    function tick(){
        $('#ticker li:first').slideUp( function () { $(this).appendTo($('#ticker')).slideDown(); });
    }
    
    var ticker=setInterval(function(){ tick () }, 2000);
    
    $('#ticker').hover(
      function() {
        clearInterval(ticker);
      }, function() {
        ticker=setInterval(function(){ tick () }, 2000);
      }
    );