javascript - 如何让 css 过渡在滚动到特定 div 之前不开始

标签 javascript jquery css scroll

当我滚动到其父 div 上时,我在尝试启动 css 转换/序列时遇到了一些问题。我已经使用 javascript 函数完成了此操作,但从未使用 css 转换。

现在我的图像甚至不会显示,更不用说从序列开始了。如果我注释掉 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;*/
	margin-left: 200px;
	width: auto;
	height: 500px;
	-webkit-animation-name: think;
	        animation-name: think;
	-webkit-animation-duration: 8s;
	        animation-duration: 8s;
    -webkit-animation-direction: normal;
            animation-direction: normal;
	-webkit-animation-fill-mode: forwards;
	        animation-fill-mode: forwards;
	/*min-height: 500px; min-width: 500px;*/
	background-repeat: no-repeat;
	/*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>

最佳答案

您可以使用jquery.visible插件轻松确定元素是否在视口(viewport)中: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 示例:https://jsfiddle.net/Zachary1748/tqh00p9n/

关于javascript - 如何让 css 过渡在滚动到特定 div 之前不开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35655955/

相关文章:

javascript - 在 Bootstrap 4 中调整输入大小

css - 浏览器如何将相邻的div布局到 float 的div

jquery - 使用 jquery 更改背景图像

javascript - 使用 Browserify 时如何公开 Google map 的回调函数?

javascript - 如何在javascript中打开文件上传对话框

javascript - Bootstrap - 禁用 <div class ="bfh-selectbox bfh-countries">

javascript - Hammer JS v2.0.4 仅在 Chrome 37.0 中无法识别 'panstart' 或 'panmove'

javascript - 如何使用angular js将表格行添加为模板

javascript - meteor JS : How do I access the previous record in a collection?

javascript - 隐藏具有特定类的所有元素并按 ID 显示