Code javascript thêm nút tới lùi các section trên trang flatsome
Thêm HTML và CSS
<ul class="nav-section"> <li> <span id="prev-section"> <i class="fas fa-angle-up"></i></span> </li> <li> <span id="next-section"> <i class="fas fa-angle-down"></i></span> </li> </ul> <style> .nav-section{ position: fixed; right: 0; bottom: 20%; list-style: none; margin: 0; padding: 0; z-index: 99999; } .nav-section span{ display: block; width: 40px; height: 40px; color: #FFF; background: #1a7a68; line-height: 35px; text-align: center; } .nav-section span:hover{ cursor: pointer; } </style>
Thêm Javascript
function scroll_to_element(elementtoScrollToID) { jQuery([document.documentElement, document.body]).animate({ scrollTop: jQuery(elementtoScrollToID).offset().top - 120 }, 500); } function find_current_section() { var currentScreenPosition = jQuery(document).scrollTop(); if (currentScreenPosition <= 100) { jQuery('#content > .section').first().addClass('current'); return false; } jQuery('#content > .section').each(function (section) { if (currentScreenPosition > jQuery(this).position().top && currentScreenPosition < jQuery(this).position().top + jQuery(this).height()) { jQuery(this).addClass('current'); return false; } }); } jQuery(function () { find_current_section(); var all_sections = jQuery('#content > .section'); jQuery(window).scroll(function () { var currentScreenPosition = jQuery(document).scrollTop(); jQuery(all_sections).each(function (section) { if (currentScreenPosition > jQuery(this).position().top && currentScreenPosition < jQuery(this).position().top + jQuery(this).height()) { jQuery(this).addClass('current'); } else { jQuery(this).removeClass('current'); } }); if (currentScreenPosition >= jQuery('#content > .section').last().position().top ){ jQuery('#content > .section').removeClass('current'); jQuery('#content > .section').last().addClass('current'); } }); jQuery('#prev-section').on('click', function (e) { e.preventDefault(); var element = jQuery('#content > .section.current').prev(); if (element) { scroll_to_element(element) } }); jQuery('#next-section').on('click', function (e) { e.preventDefault(); var element = jQuery('#content > .section.current').next(); if (element) { scroll_to_element(element) } }) });