実際に出た状況
HTML/JavaScriptで、アンカーの移動時に指定px分さらにずらしたい時に、以下の関数を埋め込んだ。
$(function () { var windowWidth = $(window).width(); var headerHight = 70; $('a[href^="#img"]').click(function() { console.log("click anchor!"); var speed = 100; var href= jQuery(this).attr("href"); console.log("href : " + href); var target = jQuery(href == "#" || href == "" ? 'html' : href); var position = target.offset().top-headerHight; console.log("position : " + position); $('body,html').animate({scrollTop:position}, speed, 'swing'); return false; }); });
すると、実行時に以下のエラーが発生。
Object doesn't support property or method 'animate'
原因
HTML側で読み込んでいるjqueryは以下のようになっていた。
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
この、 slim とは、jqueryの最低限の機能のみを持ったファイルらしい。
slimを消して、 jquery-3.3.1.min.js としてあげるとエラーが消える。
ちなみに、 min は改行スペースを省いた軽量なファイルのことです。