如何使页面跳转到其他页的某一部分且该部分距离顶部有一定的距离

如何使页面跳转到其他页的某一部分且该部分距离顶部有一定的距离,第1张

用锚链接 一、跳转到顶部 1、首先我们在网页body内最上面添加一个 2、我们再到body内,需要出现点击后转到顶部位置添加,回到顶部 二、跳转到某个地方 1、需要转到地方添加,文字,注意href值是#开头+英文字母命名

两种方法:一种是css 类中添加:target{ 偏移值 top:-20px }

另一种方法js

var handler=function(hash){ var target = document.getElementById(hash.slice(1))if (!target) returnvar targetOffset = $(target).offset().top-70$('html,body').animate({scrollTop: targetOffset}, 400)} $('a[href^=#][href!=#]').click(function(){ handler(this.hash) })if(location.hash){ handler(location.hash) }

/*

 *锚点点击跳转

 */

var AnchorClick = function (pos) {

    $("html,body").animate({ scrollTop: pos }, speed)

}

pos为你要跳转到位置:也就是你要跳转的位置举例浏览器顶部的距离,单位为px


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11861397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存