2016-05-07 1 views
0

Этот JS создает Smooth-свиток, он работает с jQuery 1.11.1, но прерывается с jQuery 1.12.3. Используя это с сайтом Wordpress и предпочитайте не загружать две версии jQuery.Smooth scroll JS, работающий с jQuery 1.11.1, но перерыв с jQuery 1.12.3

Невозможно выяснить, что обновить, чтобы заставить его работать снова.

<!--smooth scroll to anchors--> 
    <script> 
    (function($){ 
     var jump=function(e){ 
     if (e){ 
      e.preventDefault();     //prevent the "normal" behavior which would be a "hard" jump 
      var mytarget = $(this).attr("href"); //get the target 
     }else{ 
      var mytarget = location.hash;    //sets the target to the anchor part of a URL 
     } 

     $('html,body').animate(       //perform animated scrolling 
     { 
     scrollTop: $(mytarget).offset().top - 100 //get top-position of target-element and set it as scroll target and move down 100px for fixed nav 
     }, 1000,function(){       //scrolldelay: 2 seconds 
     location.hash = mytarget;     //attach the hash (#jumptarget) to the pageurl 
     }); 
     } 

     $('html, body').hide() 

     $(document).ready(function(){ 
     $('a[href^=#]').bind("click", jump); 

     if (location.hash){ 
      setTimeout(function(){ 
      $('html, body').scrollTop(0).show() 
      jump() 
      }, 0); 
     }else{ 
      $('html, body').show() 
     } 
     }); 
    })(jQuery) 
    </script> 
    <!--End smooth scroll--> 
+0

Есть ли ошибки в консоли? – Andy

ответ

0

JQuery 1.12.x не нравится a[href^=#] без кавычек вокруг #.

Он выдает непризнанную ошибку выражения.

Использование цитат рекомендуется всегда.

$('a[href^="#"]').bind("click", jump); 

Также есть проблема с вашим scrolltop во всех версиях jQuery. $ (..). offset() - null. Это происходит, когда ваш href равен # или ссылки на несуществующий идентификатор. Вы также должны проверить, что это не внешняя ссылка, и вернуться, в противном случае preventDefault остановит ее от работы.

См скрипка: https://jsfiddle.net/txau5yLf/

+0

Это исправлено. Спасибо. – justsomeone

Смежные вопросы