javascript - 图像淡入但覆盖了原来在 div 中的文本

标签 javascript jquery css

我有一个带有隐藏图像的 div 和其中的另一个 div。当用户向下滚动到 div 的中间点时,图像淡入。但是,我想让另一个 div 中的文本保持可见。这是一个消除任何混淆的 fiddle :http://jsfiddle.net/8eudrmcx/16/

请注意当图像淡入时文本是如何被遮盖的。我知道这是正常行为,但我对如何保持文本可见(和可选择)感到困惑。

HTML

<section>
    <img src="http://www.billboard.com/files/styles/promo_650/public/stylus/2029269-marilyn-manson-Agata-Alexander-617-409.jpg" class="overlay" />
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</section>

CSS

section {
  background-color: #000;
  color: #fff;
  height: 500px;
  width: 500px;
}

.overlay {
    display: none;
    position: absolute;
    height: 500px;
    width: 500px;
}

JS

function isScrolledIntoView($elem) {
   var docViewTop = $(window).scrollTop();
   var docViewBottom = docViewTop + $(window).height();

   var elemTop = $elem.offset().top;
   var elemMiddle = elemTop + $elem.height()/2;
   return docViewBottom >= elemMiddle && docViewTop <= elemMiddle;
}
$(window).scroll(function(){
   $elem = $("section"); //or what element you like
   if(isScrolledIntoView($elem)){
      $('.overlay').fadeIn();
   }
});

最佳答案

您需要使用叠加层和部分的 z-index http://jsfiddle.net/8eudrmcx/17/

   section {
        background-color: #000;
        color: #fff;
        height: 500px;
        width: 500px;
        position: relative;
        z-index: 1;
    }
    .overlay {
        display: none;
        position: absolute;
        height: 500px;
        width: 500px;
        z-index: -1;
    }

关于javascript - 图像淡入但覆盖了原来在 div 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26048296/

相关文章:

javascript - 从 javascript 的列表中选择最多 x 个项目

Python 中的 JavaScript 解析器

jQuery:将 CSS 应用于父 DIV

jquery - 无法将 event.target 转化为字符串

javascript - 制作一个像airbnb网站上的透明播放按钮图标

javascript - DataTables - 在每个 <th> 上复制类

javascript - 捕获 iframe 内的鼠标事件

javascript - 如果选择单选按钮禁用文本区域?

javascript - 如何使用 Twitter Bootstrap 2.0 验证输入字段并应用错误类?

php - 如果当前 td 有长文本,TCPDF 会忽略早期 td 的宽度