zoukankan      html  css  js  c++  java
  • 基于jQuery滚动加载页面内容效果的插件封装

    废话不多说直接上代码

    (function () {
                var scrollShow = function () {
                    this.$container = $("body");
                    if(this.$container.find(".scrollShow").length){
                        this.$scrollShow = this.$container.find(".scrollShow");
                    }
                    this.init();
                };
                var p = scrollShow.prototype;
                p.init = function () {
                    this.initHidden();
                    this.initScroll();
    
                    $(window).trigger("scroll");
                };
                //显示隐藏
                p.initHidden = function(){
                    console.log("sfahjjhj");
                    this.$container.find(".scrollShow").each(function() {
                        $(this).parent().css("overflow", "hidden");
                        $(this).data("show", false).css("opacity", 0.5);
                        if ($(this).css("position") === "static") {
                            $(this).css("position", "relative");
                        }
                        var t = $(this).css("top") === "auto" ? 0 : parseInt($(this).css("top"));
                        var top = $(this).height() > 400 ? $(this).height() / 2 : 400;
                        //将元素top值增加
                        $(this).data({"top": t, "height": top}).css({"top": t + top});
                    })
                };
                //滚动加载
                p.initScroll = function () {
                    var dom =this;
                    $(window).scroll(function(){
                        var delayIndex = 0;//延迟的时间
                        //元素的内部高度(包括padding填充)
                        var secHeight = $(this).innerHeight();//窗口的内部高度
                        var TopMax = $(this).scrollTop() + secHeight;
                        var TopMin = $(this).scrollTop() - secHeight;
    
                        dom.$container.find(".scrollShow").each(function(){
                            var height = $(this).data("height");
                            //相对于文档的偏移距离
                            var top = $(this).offset().top - height;//将元素top值减小
                            if( top <= TopMax && top >= TopMin && !$(this).data("show")){
                                var $doc = $(this);
                                $doc.data("show",true);
                                $doc.delay(delayIndex).animate({
                                    "top": $doc.data("top"),
                                    "opacity": 1
                                },500);
                                delayIndex += 500;
                            }
                        })
                    })
                };
                window.scrollShow = scrollShow;
            })();
           
    

      调用方式

           需要特效的元素加指定的选择器 scrollShow

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>scrollShow</title>
    
        <style>
            .imgBox{
                 800px;
                height: 400px;
                background: #59b0dd;
                margin: 0 auto 30px;
            }
            .imgBox img{
                 700px;
                height: 300px;
                margin: 50px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="imgBox "><img src="http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg" alt=""></div>
            <div class="imgBox scrollShow">今晚的月亮好圆呀,就想你的脸</br>今晚的月亮好圆呀,就想你的脸</br>今晚的月亮好圆呀,就想你的脸</br>今晚的月亮好圆呀,就想你的脸</br></div>
            <div class="imgBox scrollShow"><img src="http://img.zcool.cn/community/0167ec5770fb3e0000012e7eb23909.jpg@1280w_1l_2o_100sh.jpg" alt=""></div>
            <div class="imgBox scrollShow"><img src="http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg" alt=""></div>
        </div>
        <script src="jquery-1.11.3.min.js"></script>
        <script>
    $(document).ready(function () {
                //创建scrollShow实例即可
                var show = new scrollShow();
            })
        </script>
    </body>
    </html>
    

      

  • 相关阅读:
    args4 1.4.12 编写一个程序,有序打印给定的两个有序数组(含有N 个int 值)中的所有公共元素,程序在最坏情况下所需的运行时间应该和N 成正比。
    优化斐波那契数列递归的计算
    Java中BO、DAO、DO、DTO、PO、POJO、VO的概念
    并查集算法Union-Find的思想、实现以及应用
    计算机网络中一些比较重要的概念
    [转]架构初探之架构的几种设计模式
    常见排序算法的思想以及稳定性分析
    数据库基础知识整理与复习总结
    Java面试之Java集合相关问题答案口述整理
    Java面试之Java基础问题答案口述整理
  • 原文地址:https://www.cnblogs.com/iamlhr/p/10555957.html
Copyright © 2011-2022 走看看