- /*
- 调用示例:
- locate_element(jq("#surface_img"));//jq是jquery的别名引用,surface_img是dom元素id
- */
- //方法1:定位jq元素(推荐)
- function locate_element(jqobj){
- if(jqobj){ jq('html,body').animate({scrollTop:jqobj.offset().top}, 'slow');
- }
- }
- //方法2:定位jq元素
- function locate_element(jqobj){
- if(jqobj){
- var pos_obj = getElCoordinate(jqobj.get(0));
- jq('html, body').animate({scrollTop:pos_obj.top}, 'slow');
- }
- }
- //获得元素绝对定位
- function getElCoordinate(e){
- var t = e.offsetTop;
- var l = e.offsetLeft;
- var w = e.offsetWidth;
- var h = e.offsetHeight;
- while (e = e.offsetParent){
- t += e.offsetTop;
- l += e.offsetLeft;
- }
- return{
- top:t,
- left:l,
- width:w,
- height:h,
- bottom: t + h,
- right: l + w
- };
- }
- //该片段来自于http://www.codesnippet.cn/detail/1010201513777.html
来源: http://www.codesnippet.cn/detail/1010201513777.html