js 平滑定位
<script type="text/javascript"> function intval(v){
v = parseInt(v);
return isNaN(v) ? 0 : v;
} // ?取元素信息
function getPos(e){
var l = 0;
var t = 0;
var w = intval(e.style.width);
var h = intval(e.style.height);
var wb = e.offsetWidth;
var hb = e.offsetHeight;
while (e.offsetParent) {
l += e.offsetLeft + (e.currentStyle ? intval(e.currentStyle.borderLeftWidth) : 0);
t += e.offsetTop + (e.currentStyle ? intval(e.currentStyle.borderTopWidth) : 0);
e = e.offsetParent;
}
l += e.offsetLeft + (e.currentStyle ? intval(e.currentStyle.borderLeftWidth) : 0);
t += e.offsetTop + (e.currentStyle ? intval(e.currentStyle.borderTopWidth) : 0);
return {
x: l,
y: t,
w: w,
h: h,
wb: wb,
hb: hb
};
} // ?取??条信息
function getScroll(){
var t, l, w, h;
if (document.documentElement && document.documentElement.scrollTop) {
t = document.documentElement.scrollTop;
l = document.documentElement.scrollLeft;
w = document.documentElement.scrollWidth;
h = document.documentElement.scrollHeight;
}
else
if (document.body) {
t = document.body.scrollTop;
l = document.body.scrollLeft;
w = document.body.scrollWidth;
h = document.body.scrollHeight;
}
return {
t: t,
l: l,
w: w,
h: h
};
} // ?点(Anchor)?平滑跳?
function scroller(el, duration){
if (typeof el != 'object') {
el = document.getElementById(el);
}
if (!el)
return;
var z = this;
z.el = el;
z.p = getPos(el);
z.s = getScroll();
z.clear = function(){
window.clearInterval(z.timer);
z.timer = null
};
z.t = (new Date).getTime();
z.step = function(){
var t = (new Date).getTime();
var p = (t - z.t) / duration;
if (t >= duration + z.t) {
z.clear();
window.setTimeout(function(){
z.scroll(z.p.y, z.p.x)
}, 13);
}
else {
st = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.y - z.s.t) + z.s.t;
sl = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.x - z.s.l) + z.s.l;
z.scroll(st, sl);
}
};
z.scroll = function(t, l){
window.scrollTo(l, t)
};
z.timer = window.setInterval(function(){
z.step();
}, 13);
}
</script>
<div class="test">
<a name="header_1" id="header_1"></a>
<strong onclick="javascript:scroller('header_4', 800);">header_1 --> header_4</strong>
<p>
</p>
</div>
<div class="test">
<a name="header_2" id="header_2"></a>
<strong onclick="javascript:scroller('header_5', 800);">header_2 --> header_5</strong>
<p>
</p>
</div>
另外一种方式是使用jquery比较简单一些!
我们知道html有锚点定位的效果,那么如何实现平滑的定位效果呢?下面由68ecshop的技术来为您详细解答 <script type="text/javascript" src="http://code.hs-cn.com/jquery/jquery-1.7.1.min.js"></script> <div id="top" class="append_box mb20">
平滑跳转到底部:<a href="#bottom" class="smooth">滑到底部</a>
</div>
<div id="appendBox" class="append_box">
<img width="950" height="931" src="http://www.dubai.com/images/upload/Image/201407210943470ffuoj.jpg" /><br />
<img width="950" height="1000" src="http://www.dubai.com/images/upload/Image/20140721094432t9j0kt.jpg" />
</div>
<div id="bottom" class="append_box mb20">
平滑回到顶部:<a href="#top" class="smooth">回到顶部链接</a>
</div>
<script>
$(".smooth").click(function(){
var href = $(this).attr("href");
var pos = $(href).offset().top;
$("html,body").animate({scrollTop: pos}, 1000);
return false;
});
</script> //技术解答: //class="smooth":我们自己定义的一个class名称,目的是js调用该标签 //href="#top":top是要定位的标签ID名称 //$("html,body").animate({scrollTop: pos}, 1000):1000 是滑动的时间,我们可以自己调整
刷新页面之后平滑定位到指定位置
$("html, body").scrollTop(0).animate({scrollTop: $(".top-20").offset().top});
js 平滑定位的更多相关文章
- js平滑滚动到顶部,底部,指定地方
[原文链接] 采用锚点进行页面中的跳转的确很方便,但是要想增加网页的效果,可以使用jquery中的animate,实现滚动的一个动作,慢慢的滚动到你想跳转到的位置,从而看起来会非常高大上. [示例演示 ...
- ADF控件ID变化引发JS无法定位控件的解决方法
原文地址:ADF控件ID变化引发JS无法定位控件的解决方法作者:Nicholas JSFF定义的控件ID到了客户端时往往会改变.例如在JSFF中的一个的ID为"ot1",但是当这个 ...
- Auto.js 特殊定位控件方法 不能在ui线程执行阻塞操作,请使用setTimeout代替
本文所有教程及源码.软件仅为技术研究.不涉及计算机信息系统功能的删除.修改.增加.干扰,更不会影响计算机信息系统的正常运行.不得将代码用于非法用途,如侵立删! Auto.js 特殊定位控件方法 操作环 ...
- jQuery实现锚点平滑定位
一般的锚点,就是点击一个按钮或者其他元素可以实现定位效果,当然可以使用锚点实现,但是这个不够美观,没有平滑的动画过渡效果,下面就通过代码实例介绍一下利用jquery实现平滑的定位效果. <!DO ...
- 如何借助浏览器Console使用Js进行定位和操作元素
在进行Selenium自动化过程中,我们很难避免一些webdriver 很难定位到的一些元素(如:默认隐藏属性元素),那对于一些比较难定位到的元素,有什么好的解决办法? 其实我们都知道,Sel ...
- js调试-定位到函数所在文件位置
原文:http://www.cnblogs.com/52cik/p/js-console-show-source.html 在控制台输入要查找的函数名如votePost 然后回车: 函数源码粗显啦,并 ...
- js的定位实现和ip查询
sina的api var GetLocationFromSina = function (successFunc, errorFunc) { $.getScript('http://int.dpool ...
- 百度地图js版定位控件
一 概述 百度地图在最新版已加入浏览器定位控件,个人认为应该是既高德地图更新了一个浏览器也能定位功能后,百度不甘落后自己简简单单,草草写了个这个功能的定位控件 GeolocationControl 这 ...
- JS平滑无缝滚动实现———实现首页广告自动滚动效果(附实例)
本文我们实现纯JS方式的滚动广告效果. 先show一下成品: 首先是网页样式: 1. #demo { 2. background: #FFF; 3. overflow:hidden; 4. borde ...
随机推荐
- eclipse添加第三方源码
- 理解MVVM模式
1.WPF的核心是数据绑定. 2.考虑这样一个场景:界面上有一个TextBox显示Person的年龄,一个Button,点击一次Button,年龄加1. 3.做一个View,上面有TextBox和Bu ...
- Effective Objective-C 2.0 Reading Notes
1. Literal Syntax NSString *someString = @"Effective Objective-C 2.0"; NSNumber *someNumbe ...
- cocos2dx A*算法
头文件和源文件拷贝到项目中就能用了! have fun 使用cocos2dx 3.2 原理都一样 淡蓝色的点是地图 深蓝色的点是障碍物 绿色的点是路径 暗绿色的点是搜寻过的点 红色的点是按路径行走的点 ...
- 移动端折腾国外分享(facebook、twitter、linkedin)
一.前言 国内做HTML5页面,关注最多就是微信分享了,之前也写过关于微信分享的文章,可以点击查看:分享相关文章 再者,就是国内的其它分享,比如常用的新浪微博.腾讯微博.QQ空间等等,最方便的就是直接 ...
- poj 1941 The Sierpinski Fractal 递归
//poj 1941 //sep9 #include <iostream> using namespace std; const int maxW=2048; const int maxH ...
- 安装mysql问题
我想大多数人都遇到第一次安装失败或者卸载安装mysql,老是在最后一步失败:解决方法有2个:1 删除 默认路径文件C:\Documents and Settings\All Users\Applica ...
- 保持长宽比 对背景图像进行修改android:scaleType="fitXY"
关于android中ImageView的外观,即图片在其内显示出的样子,与布局文件中adjustViewBonds和scaleType属性的关系.我进行了一些探索.现跟大家共享,欢迎各位指教.分别将a ...
- 简单的新浪微博OAuth认证实现
System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY); System.setProperty(&q ...
- 【JavaScript】关于prototype
所有的对象都有constructor属性 但是只有function类型才有prototype属性----->值是一个对象,即prototype对象,所有的实例对象共享同一个prototype对象 ...