滚动条滑动到指定位置

废话不多说直接代码:

css样式代码:

 

.scroll-layer{
	width: 100%;
	background: #fff;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 1000;
}
.scroll-layer p{
	height: 4rem;
	line-height: 2rem;
	font-size: 1.3rem;
	padding-left: 10px;
}
.scroll-layer input{
	height: 3rem;
	line-height: 3rem;
	border: 1px solid #eee;
	width: 100%;
	outline: none;
	margin: 0;
	padding: 0 0 0 1rem;
}
.scroll-layer button{
	margin: 10px 10%;
	width: 80%;
	border: none;
	outline: none;
	background: lightblue;
	color: #fff;
	height: 3rem;
	line-height: 3rem;
	text-align: center;
	font-size: 1.2rem;
}

html代码:

<div class="scroll-layer">
     <p>能够输入的值:100,500,1000,1500,2000,2500,2652</p>
     <input type="text" placeholder="输入滑动位置"/>
     <button onclick="setScrollTop()">开始滑动</button>
</div>

jquery的js代码

 

 

$(function(){
	var bodyHeight = $('html,body').height();
	window.setScrollTop = function(){
		var val = $('.scroll-layer').find('input').val();
		bodyHeight >= val && $('html,body').animate({'scrollTop':val},1000);
        }
})


主要说一下注意事项:

 

1,由于位置是我们指定,所以需要用一个input输入,因此允许的输入值范围提示,输入框,触发事件按钮采用的是浮窗,漂浮在最顶部,类似一个导航。

2,输入的最大值不能超过html的高度,如果超过,不会执行滑动动画。

3,不设置的时候value值默认的为0

浮窗效果图:

效果地址:https://rattenking.github.io/demo/04/index.html

其他

[我的博客,欢迎交流!](http://rattenking.gitee.io/stone/index.html)

[我的CSDN博客,欢迎交流!](https://blog.csdn.net/m0_38082783)

[微信小程序专栏](https://blog.csdn.net/column/details/18335.html)

[前端笔记专栏](https://blog.csdn.net/column/details/18321.html)

[微信小程序实现部分高德地图功能的DEMO下载](http://download.csdn.net/download/m0_38082783/10244082)

[微信小程序实现MUI的部分效果的DEMO下载](http://download.csdn.net/download/m0_38082783/10196944)

[微信小程序实现MUI的GIT项目地址](https://github.com/Rattenking/WXTUI-DEMO)

[微信小程序实例列表](http://blog.csdn.net/m0_38082783/article/details/78853722)

[前端笔记列表](http://blog.csdn.net/m0_38082783/article/details/79208205)

[游戏列表](http://blog.csdn.net/m0_38082783/article/details/79035621)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rattenking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值