简体   繁体   中英

Underline text once user scrolls page - JQUERY

I have been looking for a way to underline text once the user scrolls down the page. I want the underline to show ONLY when the user scrolls down. I have tried using animate.css and other plugins but to no avail. Any ideas? Thank you!

Try this

<!DOCTYPE html>
<html>
<head>
<style>
p{
margin-top : 150px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $(window).on('scroll',function(){
  var a = $(window).scrollTop();
  //alert(a);
  if( a > 50) {
    $("p").css("textDecoration", "underline");
    }
    else {
       $("p").css("textDecoration", "none");
    }

  });
});
</script>
</head>
<body>
<div>
<p>If you scroll, I will underline myself.</p>
<p>If you scroll, I will underline myself.</p>
<p>If you scroll, I will underline myself.</p>
<p>If you scroll, I will underline myself.</p>
</div>

</body>
</html>

You can also try example here .

This might solve your issue:

$(document).on("scroll", function(){
    var topPX = $(window).scrollTop(); //how many pixels the user scrolled
    if(topPX > 100){
    //underlines the text once the user scrolls past 100px
        $('.text').css('text-decoration','underline');
    }
    if(topPX < 100){
    //reverts it back to normal if the user came back to to the "below 100px" position
        $('.text').css('text-decoration','none');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM