zoukankan      html  css  js  c++  java
  • 滚动条滑动到指定位置

    废话不多说直接代码:

    css样式代码:

    .scroll-layer{
    	 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;
    	 100%;
    	outline: none;
    	margin: 0;
    	padding: 0 0 0 1rem;
    }
    .scroll-layer button{
    	margin: 10px 10%;
    	 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)

  • 相关阅读:
    pta ——还原二叉树
    pta ——Topological Sort(拓扑排序)
    pta Two Stacks In One Array(简单版双向数组)
    pta Percolate Up and Down(最小堆的插入维护和删除维护)
    pta 切分表达式——写个tokenizer吧
    正则表达式基本语法详解
    pat 是否同一棵二叉搜索树
    关于java中字符、字符串、整数相加输出的小整理
    纯C语言实现的AVL树(插入&删除&前序遍历输出)
    pat Deque(双端队列)
  • 原文地址:https://www.cnblogs.com/linewman/p/9918556.html
Copyright © 2011-2022 走看看