• 响应式图片,在不同尺寸下切换不同张数


    全局标记变量

    let displaySize = 3; //默认展示图片数量
    let displayIndex = 0; //默认从第一张开始显示
    

    resize 事件

    $(window).on('resize', function () {
            let width = $(this).width();
            let $li = $('.similar .similar-home');
            let len = $li.length;
            if (width > 1020) {
                displaySize = 3;
                $li.hasClass('middle-screen') && $li.removeClass('middle-screen');
                $li.hasClass('small-screen') && $li.removeClass('small-screen');
            } else if (width > 600) {
                displaySize = 2;
                $li.hasClass('middle-screen') || $li.addClass('middle-screen');
                $li.hasClass('small-screen') && $li.removeClass('small-screen');
    
            } else {
                displaySize = 1;
                $li.hasClass('middle-screen') && $li.removeClass('middle-screen');
                $li.hasClass('small-screen') || $li.addClass('small-screen');
            }
            let wholeList = Array.prototype.slice.call($('.similar .similar-home'));
            toggleShowList(wholeList, displayIndex, displaySize);
        }).trigger('resize');
    

    封装的函数

    /**
     * 
     * @param {*} wholeList arrayLiked object dom-list
     * @param {*} displayIndex int
     * @param {*} displaySize int
     */
    function toggleShowList(wholeList, displayIndex, displaySize) {
        let len = wholeList.length;
        let showList = [];
        if (displayIndex + displaySize < len) {
            showList = wholeList.splice(displayIndex, displaySize);
        }else{
            showList = wholeList.splice(displayIndex, len).concat(wholeList.splice(0,(displayIndex+displaySize-len)));
        }
        for (let i = 0, len = wholeList.length; i < len; i++) {
            $(wholeList[i]).css('display', 'none');
        }
        for (let i = 0, len = showList.length; i < len; i++) {
            $(showList[i]).css('display', 'block');
        }
    }
    

    点击切换

    // Data.CLick 的值为navigator.userAgent.match(/iphone|ipad|ipod|android|windows phone/i)?'touchstart':'click';
    $('.similar').on(Data.CLICK, '.change-house', function () {
        let wholeList = Array.prototype.slice.call($('.similar .similar-home'));
        let maxIndex = wholeList.length;
        displayIndex += displaySize;
        if (displayIndex > maxIndex) {
            displayIndex = 0;
        }
        toggleShowList(wholeList, displayIndex, displaySize);
    });
    
  • 相关阅读:
    【leetcode】11. 盛最多水的容器
    【leetcode】8. 字符串转换整数 (atoi)
    【leetcode】6. Z 字形变换
    【leetcode】5. 最长回文子串
    【leetcode】LCP 19. 秋叶收藏集
    删除第一个节点问题
    问一个大学学习计算机这门专业的问题
    Struts文件上传页面上传后显示“连接已重置”
    2013-12-6 思杨没吃饱 饿醒了
    2013-12-7 snoopy乐园中的思杨
  • 原文地址:https://www.cnblogs.com/lakeInHeart/p/7603345.html
Copyright © 2020-2023  润新知