0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

スムーススクロール PC/SP高さそれぞれ調整

Posted at

スムーススクロール PC/SP高さそれぞれ調整

  // スムーススクロール
  if ((navigator.userAgent.indexOf('iPhone') > 0) || navigator.userAgent.indexOf('iPod') > 0 || navigator.userAgent.indexOf('Android') > 0)
    {
			jQuery(document).ready(function(){
				var adjust = -25;
				var time = 500;
				var urlHash = location.hash;
				if(urlHash) {
					jQuery('body,html').stop().scrollTop(0);
					setTimeout(function () {
						scrollToAnker(urlHash) ;
					}, 100);
				}
				jQuery('a[href^="#"]').on('click', function(event) {
					event.preventDefault();
							var decodedHash = decodeURI(this.hash);
					console.log(decodedHash);
					var hash = decodedHash == "#" || decodedHash == "" ? 'html' : decodedHash;
					scrollToAnker(hash);
					return false;
				});
				function scrollToAnker(hash) {
					var target = jQuery(hash);
					var position = target.offset().top + adjust;
					jQuery('body,html').stop().animate({scrollTop:position}, time, 'swing');
				}
			})
	}
	else {
		jQuery(document).ready(function(){
			var adjust = 0;
			var time = 500;
			var urlHash = location.hash;
			if(urlHash) {
				jQuery('body,html').stop().scrollTop(0);
				setTimeout(function () {
					scrollToAnker(urlHash) ;
				}, 100);
			}

			jQuery('a[href^="#"]').on('click', function(event) {
				event.preventDefault();
						var decodedHash = decodeURI(this.hash);
				console.log(decodedHash);
				var hash = decodedHash == "#" || decodedHash == "" ? 'html' : decodedHash;
				scrollToAnker(hash);
				return false;
			});
			function scrollToAnker(hash) {
				var target = jQuery(hash);
				var position = target.offset().top + adjust;
				jQuery('body,html').stop().animate({scrollTop:position}, time, 'swing');
			}
		})
	}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?