Advertisement
afsarwebdev

Back To Top with svg

Sep 29th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. HTML:
  2. <a href="javascript:" id="return-to-top"><?php echo file_get_contents('img/svg/icon_arrow-forward.svg'); ?></a>
  3.  
  4. SVG:
  5.  
  6. <svg data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><defs></defs><title>icon_arrow-forward</title><path class="cls-1" d="M12,4L10.6,5.4,16.2,11H4v2H16.2l-5.6,5.6L12,20l8-8Z" transform="translate(-4 -4)"/></svg>
  7. //Make svg image with the icon name(icon_forward.svg) as well extension .svg
  8.  
  9. CSS:
  10. #return-to-top {
  11. position: fixed;
  12. bottom: 10px;
  13. right: 10px;
  14. background: rgba(255, 255, 255, 0.75);
  15. width: 35px;
  16. height: 35px;
  17. display: block;
  18. text-decoration: none;
  19. z-index: 1;
  20. box-shadow: 0px 0px 0px 1px #ddd;
  21. -webkit-border-radius: 2px;
  22. -moz-border-radius: 2px;
  23. border-radius: 2px;
  24. display: none;
  25. @extend .transition;
  26. }
  27. #return-to-top {
  28. svg {
  29. height: 18px;
  30. width: 18px;
  31. fill: #000;
  32. margin: 0;
  33. position: relative;
  34. left: 8px;
  35. top: 8px;
  36. font-size: 19px;
  37. transform: rotate(-90deg);
  38. -webkit-transition: all 0.3s ease;
  39. -moz-transition: all 0.3s ease;
  40. -ms-transition: all 0.3s ease;
  41. -o-transition: all 0.3s ease;
  42. transition: all 0.3s ease;
  43. }
  44. }
  45. #return-to-top:hover {
  46. background: rgba(255, 255, 255, 1);
  47. }
  48.  
  49. JS:
  50. $(window).scroll(function() {
  51. if ($(this).scrollTop() >= 50) { // If page is scrolled more than 50px
  52. $('#return-to-top').fadeIn(200); // Fade in the arrow
  53. } else {
  54. $('#return-to-top').fadeOut(200); // Else fade out the arrow
  55. }
  56. });
  57. $('#return-to-top').click(function() { // When arrow is clicked
  58. $('body,html').animate({
  59. scrollTop : 0 // Scroll to top of body
  60. }, 500);
  61. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement