2015-12-21 167 views
0

我想知道,這樣當你懸停<h1>元素上滾動動畫停止我如何能做到這一點。如何停止滾動動畫

function scrollSlow() { 
 
    window.scrollBy(0, 1.5); 
 

 
    setTimeout(function(){ 
 
     window.requestAnimationFrame(scrollSlow); 
 
    }, 50); 
 
} 
 

 
scrollSlow();
p { 
 
    font-size: 80px; 
 
} 
 
h1 { 
 
    position:fixed; 
 
    top:10px; 
 
    left:5px; 
 
}
<h1> 
 
    Hover me 
 
</h1> 
 
<p> 
 
    Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College. 
 

 
    Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College. 
 
</p>

+0

你在這裏檢查:http://stackoverflow.com/questions/7571370/jquery-disable-scroll-when-mouse-over-an-absolute-div –

回答

0

的jsfiddle:https://jsfiddle.net/CanvasCode/ndeg1rkL/

的JavaScript

var stopScroll = false; 

$(function() { 

    function scrollSlow() { 
    if (!stopScroll) { 
     window.scrollBy(0, 1.5); 

     setTimeout(function() { 
     window.requestAnimationFrame(scrollSlow); 
     }, 50); 
    } 
    } 

    $('h1').mouseover(function() { 
    stopScroll = true; 
    }); 

    $('h1').mouseout(function() { 
    console.log("Mouseout"); 
    stopScroll = false; 
    scrollSlow(); 
    }); 

    scrollSlow(); 

}); 

我添加了一個簡單的布爾變量,它可以讓你的滾動文本停止和啓動,我還添加了鼠標懸停和mouseout事件,使滾動或禁用滾動

+1

可能不是最好的解決方案,但肯定不值得向下投票(是我發現的酸葡萄?):) –

0

可以使用自動地重複每個n毫秒的間隔,並停止其上的mouseover事件:

var interval = setInterval(function() { 
 
    window.requestAnimationFrame(function() { 
 
    window.scrollBy(0, 1.5); 
 
    }); 
 
}, 50); 
 

 
document.getElementById('my-title').onmouseover = function() { 
 
    clearInterval(interval); 
 
};
p{ 
 
    font-size: 80px; 
 
} 
 
h1{ 
 
    position:fixed; 
 
    top:10px; 
 
    left:5px; 
 
}
<h1 id="my-title"> 
 
Hover me 
 
</h1> 
 
<p> 
 
Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College. 
 
Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College.Poetry used by permission of the publishers and the Trustees of Amherst College from The Poems of Emily Dickinson, Ralph W. Franklin ed., Cambridge, Mass.: The Belknap Press of Harvard University Press. Copyright © 1998 by the President and Fellows of Harvard College. Copyright © 1951, 1955, 1979 by the President and Fellows of Harvard College. 
 
</p>

0
 function scrollSlow(){ 
       window.scrollBy(0, 1.5); 

     setTimeout(function(){ 
       window.requestAnimationFrame(scrollSlow); 
         }, 50); } 

      scrollSlow(); 



      document.getElementById("outer").onmouseover = 
      function disableScrolling(){ 
     var x=window.scrollX; 
     var y=window.scrollY; 
      window.onscroll=function(){window.scrollTo(x, y);}; 
     } 

       document.getElementById("outer").onmouseleave = function 
     enableScrolling(){ 
       window.onscroll=scrollSlow(); 
        } 

HTML :

 <h1 id="outer"> 
     Hover me 
    </h1> 
    <p id="block"> 
    Poetry used by permission of the publishers a........ 
    </p> 

在這裏你走,https://jsfiddle.net/kriz1439/ca5b9do6/

-1

您可以使用cancelAnimationFrame(id);停止動畫,其中id是來自window.requestAnimationFrame(scrollSlow)的返回值;

來源:https://css-tricks.com/using-requestanimationframe/

而且,它不是當您使用requestAnimationFrame使用的setInterval要求。你可以在上面的鏈接中閱讀它。