代码之家  ›  专栏  ›  技术社区  ›  iceiceicy

基于jQuery数据属性的鼠标滚轮和上下弹头导航

  •  0
  • iceiceicy  · 技术社区  · 6 年前

    我有一个WordPress页面,有一个带分页的滑块和一个页脚div。

    现在,分页只包含类 pagination & active ,并且每个分页选项都具有唯一性 data-page value .

    我可以知道吗 如何制作鼠标滚轮、kboard up/down&page up/down(向上/向下翻页)按钮可在分页中导航 没有任何外部插件。而且,当分页完成时, 如果用户再次按下,将显示页脚,并且没有用于分页的活动类 .

    像这样的例子 https://causeeffect.asia/services/

    无论如何,我不能100%复制,因为滑块来自WordPress插件,但下面的代码基本上就是它的样子。

    window.slide = new SlideNav();
    body, html {
    	margin: 0;
    	padding: 0;
    }
    
    .section {
    	min-height: 800px;
    	position: relative;
    	text-align: center;
    	font-family: sans-serif, arial;
    	margin: 0;
    }
    
    h1, p {
    	margin: 0;
    	font-family: sans-serif, arial;
    	font-weight: bold;
    }
    
    .text-wr {
    	position: absolute;
    	top: 50%;
    	left: 50%;
    	-webkit-transform: translate(-50%, -50%);
    	-moz-transform: translate(-50%, -50%);
    	-o-transform: translate(-50%, -50%);
    	-ms-transform: translate(-50%, -50%);
    	transform: translate(-50%, -50%);
    }
    
    #section1 {
    	background: #5c53aa;
    	color: #8d86c3;
    }
    
    #section2 {
    	background: #4bbfc3;
    	color: #81d2d5;
    }
    
    #section3 {
    	background: #283044;
    	color: #686e7c;
    }
    
    #section4 {
    	background: #ebf5ee;
    	color: #aeb9b9;
    }
    
    h1 {
    	font-size: 50px;
    	margin-bottom: 25px;
    }
    
    .title-top {
    	font-size: 60px;
    	padding-bottom: 30px;
    }
    
    .title-bottom {
    	font-size: 30px;
    }
    
    .title-tx {
    	font-size: 20px;
    	opacity: 0.8;
    }
    
    .nav {
    	position: fixed; /*left: 50%;*/
    	width: 100%;
    	top: 20px;
    	z-index: 9;
    	padding-left: 10px;
    }
    
    .nav a {
    	padding: 7px 20px;
    	border-radius: 7px;
    	margin-right: 10px;
    	background: rgba(255, 255, 255, 0.5);
    	-webkit-transition: all 0.2s ease-out;
    	-moz-transition: all 0.2s ease-out;
    	-ms-transition: all 0.2s ease-out;
    	-o-transition: all 0.2s ease-out;
    	transition: all 0.2s ease-out;
    	text-decoration: none;
    	color: black;
    	font-family: sans-serif, arial;
    	font-weight: 100;
    }
    
    .nav a.active {
    	background: rgba(0, 0, 0, 0.5);
    	color: white;
    }
    
    .nav a:hover {
    	background: rgba(255, 255, 255, 0.7);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://qmixi.github.io/slide-nav/dist/slideNav.js"></script>
    
        <div class="nav">
            <nav>
                <a href="#section1" class="pagination active" data-page="0">Link to section 1</a>
                <a href="#section2" class="pagination" data-page="1">Link to section 2</a>
                <a href="#section3" class="pagination" data-page="2">Link to section 3</a>
                <a href="#section4" class="pagination" data-page="3">Link to section 4</a>
            </nav>
        </div>
        <div id="section1" class="section">
            <div class="text-wr">
                <h1 class="title">
                    <div class="title-top">Slide Nav Example</div>
                    <div class="title-tx">Created by Piotr Kumorek</div>
                </h1>
            </div>
        </div>
        <div id="section2" class="section">
            <div class="text-wr">
                <h1 class="title">Section 2</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.</p>
            </div>
        </div>
        <div id="section3" class="section">
            <div class="text-wr">
                <h1 class="title">Section 3</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.</p>
            </div>
        </div>
        <div id="section4" class="section">
            <div class="text-wr">
                <h1 class="title">Section 4</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.</p>
            </div>
        </div>
        <footer id="footer" class="section">
            <div class="text-wr">
                <h1 class="title">Footer Area</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.</p>
            </div>
        </footer>
    0 回复  |  直到 6 年前
        1
  •  0
  •   Rohit Mittal    6 年前

    您可以使用jQuery keydown事件进行up/down/pageup/pagedown事件,也可以使用鼠标滚动。检查以下键盘键代码,如:

    document.onkeydown = function(e) {
        if (e.keyCode == 38 || e.keyCode == 33 ) { 
            //do your code here for slide next
        } else if(e.keyCode == 40 || e.keyCode == 34) { 
           //do your code for slide pervious
        }
    }
    

    对于鼠标滚动,您可以使用以下代码: How can I determine the direction of a jQuery scroll event?

    var lastScrollTop = 0;
    $(window).scroll(function(event){
       var st = $(this).scrollTop();
       if (st > lastScrollTop){
           // downscroll code
       } else {
          // upscroll code
       }
       lastScrollTop = st;
    });
    

    希望对你有帮助。