2016-04-16 41 views
0

函數animateIt更改滾動上的div文本顏色,我希望div#disable停止該函數,直到我單擊div#enable。正如你看到我寫了這段代碼,但#disable部分不起作用,所以請檢查它,讓我知道什麼是錯的。函數的禁用/啓用按鈕不起作用

var enable = true; 
 

 
$('#enable').on('click', function() { 
 
    if (!enable) { 
 
    enable = true; 
 
    animateIt(); // call again after enabled... 
 
    } 
 
}); 
 
$('#disable').click(function() { 
 
    enable = false; 
 
    $('#disable').css('background','blue'); 
 
}); 
 

 
function animateIt() { 
 
if (!enable) return; 
 
$(window).scroll(function (event) { 
 
    var scroll = $(window).scrollTop(); 
 
    if (scroll > 0 && scroll < 1000) { 
 
\t \t $('.position').css({ 
 
'color':'red))', 
 
'background':'rgba(0,40,90,1.00)' 
 
\t \t \t }) 
 
\t \t $('.position2').css({ 
 
'color':'rgba(255,248,0,1.00)', 
 
\t \t \t }) 
 
\t \t $('.position3').css({ 
 
'color':'rgba(255,0,215,1.00)', 
 
\t \t \t }) 
 
\t \t } 
 
    if (scroll > 1000 && scroll < 2000) { 
 
\t \t $('.position').css({ 
 
'color':'green', 
 
'background':'rgba(255,0,144,1.00)' 
 
\t \t \t }) 
 
\t \t $('.position2').css({ 
 
'color':'rgba(0,100,206,1.00)', 
 
\t \t \t }) 
 
\t \t $('.position3').css({ 
 
'color':'rgba(0,255,7,1.00)', 
 
\t \t \t }) 
 
\t \t } 
 
    if (scroll > 2000 && scroll < 3000) { 
 
\t \t $('.position').css({ 
 
'color':'yellow', 
 
'background':'rgba(255,0,226,1.00)' 
 
\t \t \t }) 
 
\t \t $('.position2').css({ 
 
'color':'rgba(155,0,255,1.00)', 
 
\t \t \t }) 
 
\t \t $('.position3').css({ 
 
'color':'rgba(224,224,224,1.00)', 
 
\t \t \t }) 
 
\t \t } 
 
\t 
 
    if (scroll > 3000 && scroll < 4000) { 
 
\t \t $('.position').css({ 
 
'color':'orange', 
 
'background':'rgba(255,2,6,1.00)' 
 
\t \t \t }) 
 
\t \t $('.position2').css({ 
 
'color':'rgba(69,66,179,1.00)', 
 
\t \t \t }) 
 
\t \t $('.position3').css({ 
 
'color':'rgba(124,141,245,1.0)', 
 
\t \t \t }) 
 
\t \t } 
 
    if (scroll > 4000 && scroll < 5000) { 
 
\t \t $('.position').css({ 
 
'color':'rgba(0,94,255,1.00)', 
 
'background':'rgba(255,0,226,1.00)' 
 
\t \t \t }) 
 
\t \t $('.position2').css({ 
 
'color':'rgba(224,224,224,1.00)', 
 
\t \t \t }) 
 
\t \t $('.position3').css({ 
 
'color':'rgba(155,0,255,1.00)', 
 
\t \t \t }) 
 
\t \t } 
 
\t if (scroll > 5000 && scroll < 6000) { 
 
\t \t $('.position').css({ 
 
'color':'cyan', 
 
'background':'rgba(255,238,0,1.00)', 
 
'text-shadow':'none' 
 
\t \t \t }) 
 
\t \t $('.position2').css({ 
 
'color':'rgba(176,50,0,1.0)', 
 
\t \t \t }) 
 
\t \t $('.position3').css({ 
 
'color':'rgba(100,16,5,1.00)', 
 
\t \t \t }) 
 
\t \t } 
 
\t if (scroll > 5000 && scroll < 6000) { 
 
\t \t $('.position').css({ 
 
'color':'blue', 
 
'background':'rgba(243,255,217,1.00)', 
 
\t \t \t }) 
 
\t \t $('.position2').css({ 
 
'color':'rgba(136,168,191,1.0)', 
 
\t \t \t }) 
 
\t \t $('.position3').css({ 
 
'color':'rgba(68,47,168,1.0)', 
 
\t \t \t }) 
 
\t \t } 
 
var color=$('.position').css('color'); 
 
$('#p1color').html(color); 
 
var color=$('.position2').css('color'); 
 
$('#p2color').html(color); 
 
var color=$('.position3').css('color'); 
 
$('#p3color').html(color); 
 
    
 
\t \t }); 
 
} 
 
animateIt();
body{text-align:center; height:10000px;} 
 
#disable{width:50px; height:50px; position:fixed; float:right; background:red;} 
 
.position{ 
 
color:rgba(0,255,65,1.00); 
 
background:rgba(0,40,90,1.00); 
 
font-weight:900; 
 
font-size:12px; 
 
font-family:mono; 
 
margin-top:0; 
 
overflow:hidden; 
 
display:inline-block; 
 
margin-top:40px; 
 
position:fixed; 
 

 
} 
 
.position2{ 
 
color:rgba(255,0,215,1.00); 
 
font-weight:900; 
 
font-size:12px; 
 
font-family:mono; 
 
margin-top:0; 
 
overflow:hidden; 
 
display:inline-block; 
 
margin-top:20px; 
 
    position:fixed; 
 
} 
 
.position3{ 
 
color:rgba(255,248,0,1.00); 
 
font-weight:900; 
 
font-size:12px; 
 
font-family:mono; 
 
margin-top:0; 
 
overflow:hidden; 
 
display:inline-block; 
 
margin-top:60px; 
 
position:fixed; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="disable"> 
 
</div> 
 
<div class="position"> 
 
    A 
 
    </div> 
 
<div class="position2"> 
 
    B 
 
    </div> 
 
<div class="position3"> 
 
    C 
 
    </div>

+0

我看不到div#在你的html中啓用 – yezzz

+0

我停止寫這個bc之前,div#disable不起作用,它應該是@yezzz,因爲這個函數默認是啓用的。 –

回答

2

問題在於內設置綁定到滾動事件(例如$(window).scroll(function(event) {}))當該功能被綁定,綁定的功能。當你滾動時,該函數將從此開始,每次調用動畫時,如果啓用爲true,則只是重新綁定該函數。你需要做的是在你的函數中放入啓用變量,該函數綁定到滾動事件。

$(window).scroll(function(event) { 
    if (!enable) return; 
    // otherwise do fancy color stuff 
}); 

,因此您可以簡化JavaScript來

var enable = true; 

$('#enable').click(function() { 
     enable = true; 
}); 

$('#disable').click(function() { 
     enable = false; 
}); 

$(window).scroll(function(event) { 
    if (!enable) return; 
    // rest of code 
} 

希望幫助!

+1

它按預期工作非常感謝你的答案和積極的共鳴!當然是 –

+0

!很高興它的工作(: – jpopesculian