N
N
Nikita2015-12-17 18:33:42
JavaScript
Nikita, 2015-12-17 18:33:42

Why doesn't scroll to an element work?

Scroll to element not working in wordpress. Although on normal html everything works.
The code:

(function ($) {
  $.fn.anchorAnimate = function(options) {
    var settings = $.extend({}, $.fn.anchorAnimate.defaults, options);

    return this.each(function() {
      var caller = this;
      $(caller).click(function(event) {
        event.preventDefault();
        var elementClick = $(caller).attr("href");
    var destination = $(elementClick).offset().top -60;
        $("html:not(:animated), body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
          if(0 === settings.offset) {
            window.location.hash = elementClick;
          }
        });
        return false;
      });
    });
  };

  $.fn.anchorAnimate.defaults = {
    speed: 1000
  };
}(jQuery));
$(document).ready(function(){	

$("a.slow-scroll").anchorAnimate();
  $(window).scrollTop(0);
  var hash = window.location.hash;
  hash && $('a.slow-scroll[href="' +hash+ '"]').click();
});


Link code:
<li><a class="slow-scroll" href="<?php echo get_home_url(); ?>#company">company</a></li>


When I click on the link it gives me an error:
Uncaught Error: Syntax error, unrecognized expression: http://localhost/aestetic#company

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wol_fi, 2015-12-18
@wol_fi

var elementClick = $(caller).attr("href");
    var destination = $(elementClick).offset().top -60;

In other words, you are trying to:
jQuery('http://localhost/aestetic#company');
Uncaught Error: Syntax error, unrecognized expression: http://localhost/aestetic#company(…)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question