记录下页面懒加载代码

今天京东在做图书品类的活动,买了几本心仪的书,闲暇之余看了看京东图书促销页前端代码,有很多的工具类js文件,如用于cookie、跨域、数组、业务方面等。突然看到了页面懒加载代码,做下记录。

/**
 * 图片懒加载
 */
(function(){
    if(jQuery.fn.lazyLoad) return;
    jQuery.fn.lazyLoad = function(config){
        //相关参数
        var conf = jQuery.extend({ 
                defClass:"J_imgLazyload",   //懒加载图片的class   
                defScreen:2,    //默认加载2倍屏幕高度以内的图片
                container:"body",   //img父级元素的class
                orginalSrc:"original"   //存储需要加载的图片路径
            },config||{}),
            lazyImg = (function(){  //获取所有需要懒加载的img数组
                if(conf.container.constructor == String)
                    return jQuery(conf.container + " ." + conf.defClass);
                else if(conf.container.constructor == Array){
                    var comArr = new Array();
                    for(var i = 0,len = conf.container.length;i<len;i++){
                        if(jQuery(conf.container[i] + " ." + conf.defClass).length > 0){
                            comArr.concat(jQuery(conf.container[i]));
                        }
                    }
                    return comArr;
                }else{
                    return {};
                }
            })();
        function load(){    //加载
            var top = jQuery(window).scrollTop()+document.documentElement.clientHeight*conf.defScreen;
            for(var i = 0, len = lazyImg.length;i<len;i++){
                var img = jQuery(lazyImg[i]);
                if(!img.hasClass(conf.defClass) || !img.parents('[instanceid]').length || img.is(':hidden')) continue;
                if(img.offset().top<top){
                    img.removeClass(conf.defClass);
                    img.attr("src",img.attr(conf.orginalSrc));
                    img.removeAttr(conf.orginalSrc);
                }
            }
        }
        jQuery(window).scroll(load).resize(load);
        load();
    };

    jQuery(function(){
        jQuery('body').lazyLoad();
    });
})();

对这些段代码进行了学习,发现自己的基础真心不咋地,需要更加努力的学习,constructor属性并不晓得是什么意思。谷歌了下

它的用法类似与typeof,但是它比typeof更加的精确,特别是在处理函数对象中时。并且constructor它是prototype中的一个属性,prototype包含2个属性一个就是constructor另一个是__proto__,这个属性感觉是处理js的原型链的。

下面是我自己在项目写的懒加载,用于移动web

lazyImg:function(){
        var winH = $(window).height();
        var lazyImg = $('.lazyimg');    //img的父级元素
        scrollTop();
        $(document).on('scroll',function(){
            scrollTop();
        });
        function scrollTop(){
            var top = $(window).scrollTop();
            lazyImg.each(function(){
                var floorTop = $(this).offset().top,
                    scrollH = floorTop-winH;
                top >= scrollH && imgSrc($(this));
            });
        }
        function imgSrc(elem){
            elem.find('img').each(function() {
                var _this = $(this);
                var imgSrc = _this.attr('data-lazyimg');
                if (!publicFun.isNull(imgSrc)) {
                    _this.removeAttr('data-lazyimg');
                    var image = new Image();
                    image.src = imgSrc;
                    if (image.complete) {   //图片有缓存
                        _this.attr("src",imgSrc);
                        _this.fadeIn(600);
                    } else {
                        image.onload = function() { //图片获取成功
                            _this.attr("src",imgSrc);
                            _this.fadeIn(600);
                        };
                    }
                }
            });
        }
    }

 

转载于:https://www.cnblogs.com/skyHF/p/4927312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的微信小程序制作学习计划打卡记录页面代码示例: ```html <!-- index.wxml --> <view class="container"> <view class="header">学习计划打卡记录</view> <view class="content"> <view class="plan"> <view class="plan-title">今日计划</view> <view class="plan-item" wx:for="{{planList}}" wx:key="index"> <checkbox value="{{item.checked}}" bindchange="onPlanChange">{{item.title}}</checkbox> </view> </view> <view class="record"> <view class="record-title">打卡记录</view> <view class="record-item" wx:for="{{recordList}}" wx:key="index"> <checkbox checked="{{item.checked}}" disabled="{{!item.enable}}">{{item.date}}</checkbox> </view> </view> </view> </view> ``` ```css /* index.wxss */ .container { display: flex; flex-direction: column; height: 100%; } .header { background-color: #007aff; color: #fff; font-size: 18px; text-align: center; line-height: 50px; } .content { flex: 1; display: flex; flex-direction: column; padding: 20px; } .plan { margin-bottom: 20px; } .plan-title { font-size: 16px; font-weight: bold; margin-bottom: 10px; } .plan-item { display: flex; align-items: center; margin-bottom: 10px; } .record-title { font-size: 16px; font-weight: bold; margin-bottom: 10px; } .record-item { display: flex; align-items: center; margin-bottom: 10px; } ``` ```javascript // index.js Page({ data: { planList: [ { title: '阅读一篇英文文章', checked: false }, { title: '学习一小时编程', checked: false }, { title: '完成一个小项目', checked: false }, ], recordList: [], }, onPlanChange(event) { const { index } = event.currentTarget.dataset; const { value } = event.detail; this.setData({ [`planList[${index}].checked`]: value, }); this.updateRecordList(); }, updateRecordList() { const recordList = []; const today = new Date().toLocaleDateString(); this.data.planList.forEach((plan, index) => { if (plan.checked) { recordList.push({ date: today, checked: true, enable: true, }); } else { const lastRecord = this.data.recordList[index - recordList.length]; if (lastRecord && lastRecord.date === today) { recordList.push(lastRecord); } else { recordList.push({ date: today, checked: false, enable: false, }); } } }); this.setData({ recordList }); }, }); ``` 这个页面包含两个部分:学习计划和打卡记录。学习计划部分使用 `checkbox` 组件表示每日计划,当用户勾选计划后,会更新打卡记录部分的内容。打卡记录部分使用 `checkbox` 组件表示每天的打卡情况,已经打卡过的日期会被禁用。 在 `onPlanChange` 方法中,我们处理计划勾选状态的变化,并且调用 `updateRecordList` 方法更新打卡记录。在 `updateRecordList` 方法中,我们根据计划的勾选状态和之前的打卡记录生成新的打卡记录。这个方法会在页面加载和计划勾选状态变化时被调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值