2016-08-19 23 views
0

有這個代碼,它檢查類是否在視圖中,如果它是添加一個類,但出於一些外在原因它確實工作。我正在嘗試添加類框 - 活動只有在視圖中的div。Jquery添加一個類,當元素在視圖中

我已經有一段時間了,你們可以告訴我代碼有什麼問題嗎?和一個可能的修復或我可以修復它。

codepen:http://codepen.io/salman15/pen/rLRZrJ

jQuery的

$(document).ready(function() { 

    $('#next').click(function() { 
    if ($('.in1,.in2,.in3').next('.t1,.t2,.t3').length) { 

     $('.t1').animate({ 
     left: '-1000px' 
     }) 
     $('.in1').removeClass('in1') 
     .next('.t1') 
     .addClass('in1'); 


     $('.t2').animate({ 
     right: '-1000px' 
     }) 
     $('.in2').removeClass('in2') 
     .next('.t2') 
     .addClass('in2'); 

     $('.t3').animate({ 
     bottom: '-1000px' 
     }) 
     $('.in3').removeClass('in3') 
     .next('.t3') 
     .addClass('in3'); 

    } 
    }); 

    $('#prev').click(function() { 
    if ($('.in1,.in2,.in3').prev('.t1,.t2,.t3').length) { 

     $('.t1').animate({ 
     left: '-1000px' 
     }) 
     $('.in1').removeClass('in1') 
     .prev('.t1') 
     .addClass('in1'); 


     $('.t2').animate({ 
     right: '-1000px' 
     }) 
     $('.in2').removeClass('in2') 
     .prev('.t2') 
     .addClass('in2'); 

     $('.t3').animate({ 
     bottom: '-1000px' 
     }) 
     $('.in3').removeClass('in3') 
     .prev('.t3') 
     .addClass('in3'); 

    } 
    }); 

}); 

$.fn.isVisible = function() { 
    // Am I visible? 
    // Height and Width are not explicitly necessary in visibility detection, the bottom, right, top and left are the 
    // essential checks. If an image is 0x0, it is technically not visible, so it should not be marked as such. 
    // That is why either width or height have to be > 0. 
    var rect = this[0].getBoundingClientRect(); 
    return (
     (rect.height > 0 || rect.width > 0) && 
     rect.bottom >= 0 && 
     rect.right >= 0 && 
     rect.top <= (window.innerHeight || document.documentElement.clientHeight) && 
     rect.left <= (window.innerWidth || document.documentElement.clientWidth) 
    ); 
}; 
if ($('.box').isVisible()) { 
      setTimeout(function(){ 
      $('.box').removeClass('box-active')}, 4000); 
} else{ 
       setTimeout(function(){ 
      $('.box').addClass('box-active')}, 4000); 
}; 
+0

你爲什麼要加入你的完整的項目? :P你可以只添加相關的代碼 –

+0

有時我得到的HTML請求:D – Salman

+0

你重複點擊功能一遍又一遍...... jquery是一種混亂。你只需要一個點擊功能..然後if語句。你的方式,我很驚訝的事情發生,如你所料。 – Scott

回答

2

你爲什麼不使用來自.animate整理的事件? 完成一個動畫之後,您可以輕鬆地將任何元素添加到任何元素。

參考:

例子:

var clicked = 1; 
 
$("button").click(function(){ 
 
    /* just for the demo */ 
 
if(clicked==4){ 
 
    clicked = 2; 
 
    $(".inner").css("margin-left","0px"); 
 
    } 
 
else clicked++; 
 
    /* - */ 
 
    
 
    if($(".box-active").length == 1) $(".box-active").removeClass("box-active"); 
 
    
 
    $(".inner").animate({marginLeft :"-=250px"},"slow",function(){ 
 
    //WHEN ANIMATION IS COMPLETE 
 
    // Add the box-active class 
 
    $("div.a"+clicked+"").addClass("box-active"); 
 
    }); 
 

 
});
.outer{ 
 
    width:250px; 
 
    height:100px; 
 
    overflow:hidden; 
 
} 
 
.inner{ 
 
    width:1000px; 
 
    height:100px; 
 
    margin-left:0px; 
 
} 
 
.inner > div{ 
 
    width:250px; 
 
    height:100px; 
 
    float:left; 
 
} 
 

 
.a1{ 
 
    background:blue; 
 
} 
 
.a2{ 
 
    background:red; 
 
} 
 

 
.a3{ 
 
    background:green; 
 
} 
 
.a4{ 
 
    background:grey; 
 
} 
 

 
.box-active{ 
 
    background:cyan !important; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button>Next</button> 
 
<div class="outer"> 
 
    <div class="inner"> 
 
    <div class="a1 box-active"></div> 
 
    <div class="a2"></div> 
 
    <div class="a3"></div> 
 
    <div class="a4"></div> 
 
    </div> 
 
</div>

+0

我想添加一個類,當元素在視圖中,但它不工作。轉換代碼工作正常。 – Salman

+0

我不太確定你在找什麼。還有一些動畫開始的事件以及在動畫製作過程中觸發的步驟/進度事件。如果您正在尋找第一個像素在動畫製作時可見的時刻,那麼您應該嘗試:獲取用戶窗口的高度和寬度,通過progresssteps獲取當前動畫元素的位置,並檢查頂部/右側/左側/底部(取決於你的元素從哪一側滑入)側面已經在用戶的視圖中可見 – Chris

+0

好吧,沒有看到你已經在做這件事。所以你要檢查的是$(.box)是否可見,問題是你有3個div的類,所以它總是檢查最後一個,如果它在初始時不可見,它總是會說它不是,即使第一個是。當你添加你的班級時也是如此。 $(「。box」).addClass/removeClass將適用於所有或僅最後一個.box – Chris

相關問題