Javascript 在android中单击jquery滚动到div顶部
我有一个不同的带溢出设置的滚动和按钮,当点击会滚动回到顶部Javascript 在android中单击jquery滚动到div顶部,javascript,jquery,html,css,scroll,Javascript,Jquery,Html,Css,Scroll,我有一个不同的带溢出设置的滚动和按钮,当点击会滚动回到顶部 <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script src="http://code.jquery.com/jquery.min.js"></script> <script src="htt
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<meta charset="utf-8">
<style>
.pages{
position:absolute;
top:30px;
bottom:0px;
left:0px;
right:0px;
border:0;
overflow:scroll;
}
.pages div{
position:relative;
width:100%;
height:300px;
border:solid 1px #CDCDCD;
border-top-style:none;
border-left-style:none;
border-right-style:none;
}
</style>
<script>
$(document).ready(function(e) {
$('#new_pages').click(function(event){
event.preventDefault();
//add new page data
//scroll to top
$('#mag_pages').animate({
scrollTop:0
},900);
});
});
</script>
</head>
<body>
<div class="new_pages" id="new_pages">
<div id="count">10+</div>
<div id="text">New Pages</div>
</div>
<div class="pages" id="mag_pages">
<div>Page 1</div>
<div>Page 2</div>
<div>Page 3</div>
<div>Page 4</div>
<div>Page 5</div>
</div>
</body>
</html>
.页{
位置:绝对位置;
顶部:30px;
底部:0px;
左:0px;
右:0px;
边界:0;
溢出:滚动;
}
.pages分区{
位置:相对位置;
宽度:100%;
高度:300px;
边框:实心1px#CDCDCD;
边框顶部样式:无;
左边框样式:无;
右边框样式:无;
}
$(文档).ready(函数(e){
$(“#新建页面”)。单击(函数(事件){
event.preventDefault();
//添加新页面数据
//滚动到顶部
$(“#杂志页面”)。设置动画({
滚动顶部:0
},900);
});
});
10+
新页面
第1页
第2页
第3页
第4页
第5页
在谷歌搜索了几次之后,我仍然没有进一步的进展。scrollTop:0是所有帖子的全部内容。不管动画与否对我来说都不起作用。有没有其他方法可以实现这一点。
当我在移动设备上测试滚动功能不工作时,代码在计算机上运行良好。
手机必须有不同的目标吗?我在我的一个项目中使用了这样的代码,它可以工作,请尝试修改您的代码,如下所示:
$(document).ready(function(e) {
$('#new_pages').click(function(event){
event.preventDefault();
//add new page data
//scroll to top
$('html, body').animate({
scrollTop:$('#mag_pages').offset().top
},900);
});
});
也许JQuery滚动或动画功能在移动设备中存在一些问题 尝试使用纯JS,而不是JQuery:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<meta charset="utf-8">
<style>
.pages{
position:absolute;
top:30px;
bottom:0px;
left:0px;
right:0px;
border:0;
overflow:scroll;
}
.pages div{
position:relative;
width:100%;
height:300px;
border:solid 1px #CDCDCD;
border-top-style:none;
border-left-style:none;
border-right-style:none;
}
</style>
<script>
function newPagesClicked(){
//add new page data
//scroll to top
scrollToTop
}
function scrollToTop(scrollDuration) {
var scrollStep = -window.scrollY / (scrollDuration / 15),
scrollInterval = setInterval(function(){
if ( window.scrollY != 0 ) {
window.scrollBy( 0, scrollStep );
}
else clearInterval(scrollInterval);
},15);
}
</script>
</head>
<body>
<div class="new_pages" onclick="newPagesClicked()" id="new_pages">
<div id="count">10+</div>
<div id="text">New Pages</div>
</div>
<div class="pages" id="mag_pages">
<div>Page 1</div>
<div>Page 2</div>
<div>Page 3</div>
<div>Page 4</div>
<div>Page 5</div>
</div>
</body>
</html>
.页{
位置:绝对位置;
顶部:30px;
底部:0px;
左:0px;
右:0px;
边界:0;
溢出:滚动;
}
.pages分区{
位置:相对位置;
宽度:100%;
高度:300px;
边框:实心1px#CDCDCD;
边框顶部样式:无;
左边框样式:无;
右边框样式:无;
}
函数newpageslicked(){
//添加新页面数据
//滚动到顶部
滚动托普
}
函数scrollToTop(滚动持续时间){
var scrollStep=-window.scrollY/(scrollDuration/15),
scrollInterval=setInterval(函数(){
如果(window.scrollY!=0){
scrollBy(0,scrollStep);
}
else clearInterval(滚动间隔);
},15);
}
10+
新页面
第1页
第2页
第3页
第4页
第5页
适用于我:奇怪的是,移动设备是否必须以不同的目标为目标检查您的控制台(F12)您肯定有一些JS错误,这些错误会停止您这边的JS脚本执行。没有,它在我的笔记本电脑上运行,但当我在我的设备上测试时,它不会滚动回顶部。您使用的设备是什么?在桌面上工作正常,但在移动设备上不工作-请参阅更新