2016-02-26 22 views
1

我遇到了一些問題,試圖讓我的CSS過渡/序列開始,一旦我滾動其父div。我已經完成了JavaScript功能,只是從來沒有一個CSS過渡。如何獲得一個css過渡,直到滾動到一個特定的div才啓動

現在我的圖片甚至不會顯示,更不用說從序列開始了。如果我註釋掉javascript,那麼序列將正常播放。

當我進入父div section1時,我該如何獲得css轉換?

我把這個在評論中的jsfiddle,因爲它更容易看到這一點。

/*$("#think").hide(); 
 

 
//init scrolling event heandler 
 
$(document).scroll(function() { 
 

 
    var docScroll = $(document).scrollTop(), 
 
    boxCntOfset = $("#section1").offset().top - 100; 
 

 

 
    //when rich top of boxex than fire 
 
    if (docScroll >= boxCntOfset) { 
 

 
    $("#think").fadeIn(200) 
 

 
    } else { 
 
    $("#think").fadeOut(200) 
 

 
    } 
 
})*/ 
 

 

 
//Scroll for think images 
 
/*$("#think").hide(); 
 
$(function() { 
 
    var oTop = $('##section1').offset().top - window.innerHeight; 
 
    $(window).scroll(function() { 
 

 
    var pTop = $('body').scrollTop(); 
 
    console.log(pTop + ' - ' + oTop); 
 
    if (pTop > oTop) { 
 
     $("#think").fadeIn(200) 
 
    } 
 
    }); 
 
});*/
#red { 
 
    width: 100%; 
 
    height: 700px; 
 
    background:red; 
 
} 
 
#section1 { 
 
    width: 100%; 
 
    height: 600px; 
 
    background:blue; 
 
} 
 
#think { 
 
/*opacity: 0;*/ 
 
\t margin-left: 200px; 
 
\t width: auto; 
 
\t height: 500px; 
 
\t -webkit-animation-name: think; 
 
\t   animation-name: think; 
 
\t -webkit-animation-duration: 8s; 
 
\t   animation-duration: 8s; 
 
    -webkit-animation-direction: normal; 
 
      animation-direction: normal; 
 
\t -webkit-animation-fill-mode: forwards; 
 
\t   animation-fill-mode: forwards; 
 
\t /*min-height: 500px; min-width: 500px;*/ 
 
\t background-repeat: no-repeat; 
 
\t /*background-size: 100% auto;*/ 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="red"></div> 
 
<div class="section" id="section1"> 
 
    <div id="section1-right-container"> 
 
    <div id="think"></div> 
 
    </div> 
 
</div>

+0

https://jsfiddle.net/#&togetherjs=yjijgkGMs3 – Becky

+0

可能重複:http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433 – Zach

+0

我不只是試圖弄清楚是否有東西在視口中?我想初始化一些東西?那怎麼樣? – Becky

回答

0

可以使用jquery.visible插件可以輕鬆地確定元素中視:http://www.jsdelivr.com/projects/jquery.visible

接下來,您將需要確定是否是在一個窗口中滾動功能可見光和添加一個包含動畫的css類:

$(window).on("scroll", function(){ 
    // Determine if the element is in the viewport 
    if($('.body-main').visible(true)) { 
     $('.body-main').addClass("think"); 
    } 
}); 

JSfiddle考試PLE:https://jsfiddle.net/Zachary1748/tqh00p9n/

+0

我取得了一些進展,它在這個小提琴中起作用,但是當我在實況網頁的css文件中有'#think.start'時,它甚至不會顯示圖像。 https://jsfiddle.net/nn0mazs6/#&togetherjs=PlyTzciKDM – Becky

+0

對我來說似乎它工作? – Zach

+0

無論何時我有#think.start在我的css文件中的實時頁面上,它甚至不顯示圖像......它在jsfiddle中工作,但不會在我的實時頁面上,只要我在#think後面放置.start。 – Becky

相關問題