html - Read more link with pure JavaScript -


i trying create simple read more example. consists of paragraph , button half of paragraph enclosed in span tag set hidden. when user clicks on read more button hidden span shows up. have got working code want fade in effect jquery pure javascript. please help.

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>

starting here

span.style.opacity = 0; 

you'll need gradually transition opacity here.

span.style.opacity = 1; 

you'll need use asynchronous construct (settimeout/setinterval/requestanimationframe) iterating, because synchronous 1 (while/for/for-in/foreach) block main thread, preventing browser rendering element updated opacity.

function fadein(element) {    function transition() {     if(element.style.opacity < 1) {       requestanimationframe(transition);       element.style.opacity = number(element.style.opacity) + 0.05;     }   }    transition(); } 

alternatively, if you're happy use css (rather pure js) can classes , transitions.

.out {   opacity: 0;   transition-duration: 0.5s; }  .in {   opacity: 1;   transition-duration: 0.5s; } 

make sure element has out class when arrives in dom, when you're ready fade in, swap in class , browser handle animation you.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -