【发布时间】:2012-08-09 17:45:07
【问题描述】:
问题很简单。
如何通过 javascript 将浏览器滚动到所需元素或所需位置?
非常感谢您的帮助!
【问题讨论】:
标签: javascript html browser
问题很简单。
如何通过 javascript 将浏览器滚动到所需元素或所需位置?
非常感谢您的帮助!
【问题讨论】:
标签: javascript html browser
到一个元素:
document.getElementById('id').scrollIntoView();
Has cross browser support 可能是最简单的方法......
或到特定位置:
window.scroll(x,y);
【讨论】:
//Finds y value of given object
function findPos(obj) {
var curtop = 0;
if (obj.offsetParent) {
do {
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return [curtop];
}
}
//Get object
var SupportDiv = document.getElementById('customer_info');
//Scroll to location of SupportDiv on load
window.scroll(0,findPos(SupportDiv));
【讨论】: