实现代码如下
var url = window.location.toString();
var urlArray = url.split('#');
var id = urlArray[urlArray.length-1];
if(id == 'in8'){
var t = $('#in8').offset().top.toFixed(2);
console.log(t);
$(document).scrollTop(t);
}
经过在pc端测试没问题,移动端定位不准
经过一段时间的测试,发现了问题所在,主要是由于手机端的网速较慢,DOM资源不能有效加载,使得js代码定位不准。
随后进行改进,做了一下延迟基本没问题了。
** 改进后代码如下: **
$(function(){
setTimeout(toGlobal,1500);
function toGlobal(){
var url = window.location.toString();
var urlArray = url.split('#');
var id = urlArray[urlArray.length-1];
if(id == 'in8'){
var t = $('#in8').offset().top.toFixed(2);
console.log(t);
$(document).scrollTop(t);
}
}
});