阅读纯JavaScript的更多链接

时间:2016-02-08 02:42:42

标签: javascript html css fadein

我正在尝试创建一个简单的Read More示例。它由一个段落和一个按钮组成,段落的一半用span标记括起来,最初设置为隐藏。当用户单击“阅读更多”按钮时,将显示隐藏的跨度。我有工作代码,但只是希望像JQuery一样淡化效果,但使用纯Javascript。有人请帮忙。

var span = document.getElementsByTagName('span')[0];
var hideshow = document.getElementById('hideshow');

span.style.display = 'none';

hideshow.onclick = function() {
  span.style.display = 'block';
};
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa maiores dolore earum ducimus molestiae, aut. <span>Quisquam consequuntur, maiores et, doloremque atque provident similique consequatur totam voluptas vitae veniam, molestiae laborum.</span></p>

<button id="hideshow">Read More</button>

3 个答案:

答案 0 :(得分:5)

一种方法是使用CSS3 transition来转换元素的不透明度。

在下面的示例中,单击按钮时,类fade-in会添加到子span元素中。

var button = document.querySelector('.read-more');

button.addEventListener('click', function(event) {
  var span = event.target.previousElementSibling.querySelector('span');
  span.classList.add('fade-in');
});
.show-more span {
  display: inline-block;
  height: 0;
  overflow: hidden;
  transition: opacity 2s;
  opacity: 0;
}
.show-more span.fade-in {
  height: auto;
  opacity: 1;
}
<p class="show-more">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa maiores dolore earum ducimus molestiae, aut. <span>Quisquam consequuntur, maiores et, doloremque atque provident similique consequatur totam voluptas vitae veniam, molestiae laborum.</span></p>
<button class="read-more">Read More</button>

如果您想要一种适用于多个元素的方法,您还可以使用以下内容:

var buttons = document.querySelectorAll('.read-more');

for (var i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener('click', function(event) {
    var span = event.target.previousElementSibling.querySelector('span');
    span.classList.add('fade-in');
  });
}
.show-more span {
  display: inline-block;
  height: 0;
  overflow: hidden;
  transition: opacity 2s;
  opacity: 0;
}
.show-more span.fade-in {
  height: auto;
  opacity: 1;
}
<p class="show-more">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa maiores dolore earum ducimus molestiae, aut. <span>Quisquam consequuntur, maiores et, doloremque atque provident similique consequatur totam voluptas vitae veniam, molestiae laborum.</span></p>
<button class="read-more">Read More</button>

<p class="show-more">Another shorter paragraph. <span>Quisquam consequuntur, maiores et, doloremque atque provident similique consequatur totam voluptas vitae veniam, molestiae laborum.</span></p>
<button class="read-more">Read More</button>

答案 1 :(得分:4)

从这里开始

span.style.opacity = 0;

您需要逐渐将不透明度转换为此处。

span.style.opacity = 1;

您需要使用异步构造(setTimeout / setInterval / requestAnimationFrame)进行迭代,因为同步构造(while / {{1 }} / for / for-in)将阻止主线程,阻止浏览器实际呈现具有更新的不透明度的元素。

forEach

或者,如果您乐意使用CSS(而不是纯JS),则可以使用类和转换执行此操作。

function fadeIn(element) {

  function transition() {
    if(element.style.opacity < 1) {
      requestAnimationFrame(transition);
      element.style.opacity = Number(element.style.opacity) + 0.05;
    }
  }

  transition();
}

确保元素在到达DOM时具有.out { opacity: 0; transition-duration: 0.5s; } .in { opacity: 1; transition-duration: 0.5s; } 类,然后当您准备淡入时,将其交换为out类,浏览器将为你处理动画。

答案 2 :(得分:0)

var duration = 2000; // msecs

document.getElementById('hideshow').onclick = () => {
   requestAnimationFrame((start_time) => {
      var anim = (time) => {
         var p = (time - start_time) / duration;
         (p < 1) && requestAnimationFrame(anim);
         span.style.opacity = 1 - p;
      }
      anim(start_time);
   })
}