zoukankan      html  css  js  c++  java
  • jquery scroll()滚动条事件

    最近做项目用了到scroll滚动条事件,花了很多时间做搜索,没有很好的先整理思考后再去搜索,做编码事件时,没有事先考虑清楚,理清思路,先做简单的测试成功后,再完成其他的实现。

    1.scroll()事件

    当用户滚动指定的元素时,会发生scroll事件。适用于所有可滚动的元素和window对象(浏览器窗口)

    $(select).scroll([Data],fn);

    当(浏览器窗口)页面滚动条变化时,执行的函数,JQuery代码:

    $(window).scroll(function(){

      //do something...

    });

    2.scrollTop 获取匹配元素相对滚动条顶部的偏移

    scrollTop(val)

    获取页面滚动条的具体值:$(document).scrollTop();

    设置相对滚动条的偏移值:$(document).scrollTop(300);

    3.offset() 获取匹配元素在当前视口的相对偏移。

    offset([coordinates])

    获取元素的相对偏移:$(".bg02").offset().top;

    设置元素的相对偏移:$(".bg02").offset({"top":1000, "left":0});

    4.需求:当(浏览器窗口)页面滚动条值小于300px时隐藏div,大于300px时显示div

    $(window).scroll(function(){

      //var ds = document.documetElement.scrollTop || document.body.scrollTop;  //js兼容获取滚动条

      if($(document).scrollTop() < 300){

        $(".position").css({"display":"none"});

      }else{

        $(".position").css({"display":"block"});

      }

    });

    5.返回顶部和元素定位(1-10)

    $(".position ul li").click(function(){

      var num_index = $(this).index() + 1;

          if(num_index < 10){

          $("html,body").animate({scrollTop:$(".bg0" + num_index).offset().top},800);

          }else if(num_ineex ==10){

        $("html,body").animate({$(".bg"+num_index).offset().top},800);

      }else if(num_index == 11){

        $("html,body").animate({scrollTop:0},800);

          }else{

        return false;

          }

    });

  • 相关阅读:
    MyBatis入门基础
    复制复杂链表
    二叉树中和为某一值的所有路径
    树的层次遍历
    Statement, PreparedStatement和CallableStatement的区别
    JSP有哪些动作?
    latex 输入矩阵
    Struts简单入门实例
    在Eclipse里面配置Struts2
    Windows使用Github
  • 原文地址:https://www.cnblogs.com/alantao/p/4605642.html
Copyright © 2011-2022 走看看