html页面定位,指定位置(暂时整理点,以后有更好的再换)

        因为页面有点长,所以页面点位试了一下,可以的。页面定位,两种情况:1、A页面跳到B页面,滚动到页面的任何地方;2、在一个页面内由于页面太长,需要定位。

情况1:          

首先在A页面创建一个锚点

<body><a href="b.html#pos" target="_blank">点击跳转</a><body>

然后在B页面定义这个锚点

<body><div name="pos" id="pos"></div></body>

同时把name和id都写上这样兼容性好。

情况2:

1使用锚点 的方式

2jQuery的aminate的方法

 <body>
 <script type="text/javascript">
 function click_scroll() {
  var scroll_offset = $("#pos").offset();  //得到pos这个div层的offset,包含两个值,top和left
  $("body,html").animate({
   scrollTop:scroll_offset.top  //让body的scrollTop等于pos的top,就实现了滚动
   },0);
   //0是代表移动的时间,1000代表1秒
 }
 </script>
 <input type="button" value="点击button跳转" onclick="click_scroll();" />
 <div id="pos">滚动到这里</div>
<body>

3使用聚焦的方式,

<a href="javascript:void(0)" document.getElementById('abc').focus();">快速定位1</a> //让abc获取焦点


4通过scrollIntoView方法来实现定位:

<a href="javascript:void(0)" onclick="document.getElementById('am').scrollIntoView();">快速定位1</a>

5通过window.location.hash实现:

<a href="javascript:void(0)" onclick="window.location.hash='am'">快速定位2</a>

个人倾向与Jquery的aminate效果好!毕竟是可设动画的。

转载于:https://my.oschina.net/shaonan/blog/598257

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值