jQuery3 animateで指定位置まで移動するサンプルコードです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <div id="test"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> animate_scroll($('#test')); // targetまでanimateにて移動 function animate_scroll(target) { if (!target) { return false; } var target_position = target.offset().top; // 移動位置取得 var adjustment_num = 20; // 位置調整(targetから上に20px) target_position = ((target_position - adjustment_num) > 0) ? (target_position - adjustment_num) : 0; $('html, body').animate({scrollTop: target_position}, 400, 'linear'); } </script> |