search
HomeWeb Front-endHTML Tutorial[Original]zepto creates a mobile swipe screen plug-in_html/css_WEB-ITnose

Recently I have been busy replacing jquery 2 in the project with zepto

Because I didn’t want to reference too many zepto packages, I spent some time

zepto is really It’s a lot streamlined, and the source code looks really comfortable

It happened that the project needed a swipe screen plug-in, so I wrote one using zepto

The logic is actually very simple, but there is no When I thought of the test, there were many bugs in touchmove on the old version of the android device browser

It took a while to make compatibility

Rendering

Style 1

Style 2

Call

Normally it should be the html code generated in the background , but I still wrote a set of methods for operating tab pages

The call is simple as follows:

<link rel="stylesheet" href="kslider.css" type="text/css"/><script type="text/javascript"  src="http://zeptojs.com/zepto.js"></script><script type="text/javascript"  src="zepto.kslider.js"></script><script type="text/javascript">    var k;    $(function () {            /*        参数:config        change:tab页变更事件            参数e: 当前页码        tick:自动滚动间隔时间毫秒 (不设置则不自动滚动)        maxWidth:容器最大宽度 (默认有100%)        minWidth:容器最小宽度 (默认有100%)        className:样式类名            "ks_wt_1" 标题栏-方形 (默认)            "ks_wt_2" 标题栏-小圆形            或者你自定义的类名        */        k = $("#divs1").slider({ change: function (e) { console.log(e); }, maxWidth: 360, minWidth: 300 });        //js添加一页并且跳转到第4页        k.add("标题", "内容").tab(3);        //删除页        //k.remove(0);        //小圆形按钮标题  每隔3秒自动滚动 myimg:自己写的css类,控制里面图片大小        $("#divs2").slider({ maxWidth: 300, className: "ks_wt_2 myimg", tick: 3000 });    });</script>

html

<div id="divs1" class="kslider">    <ul class="ks_wt">    <li class="ks_t2">标题1</li>    <li>标题2</li>    <li>标题3</li>    </ul>    <div class="ks_dbox ks_ts">        <div class="ks_warp">            <ul>                <li>text1</li>                <li>text1</li>                <li>text1</li>                <li>text1</li>                <li>text1</li>                <li>text1</li>                <li>text1</li>                <li>text1</li>            </ul>        </div>        <div class="ks_warp">            <img  src="/static/imghwm/default1.png"  data-src="img/img1.jpg"  class="lazy" / alt="[Original]zepto creates a mobile swipe screen plug-in_html/css_WEB-ITnose" >        </div>        <div class="ks_warp">            <ul>                <li>text3</li>                <li>text3</li>                <li>text3</li>                <li>text3</li>                <li>text3</li>                <li>text3</li>            </ul>        </div>    </div></div>

Specific code

css

/*    kslider.css    lxk 2014.08.14    www.cnblogs.com/wingkun*/body{margin:0px;text-align:center;font:12px 微软雅黑;}.kslider{width:100%;overflow:hidden;margin:0 auto;background:#f0f0f0;}.kslider .ks_warp{width:100%;}.kslider .ks_ts{-webkit-transition:500ms;}.kslider .ks_dbox{width:100%;display:-webkit-box;text-align:left;}.kslider .ks_wt{display:-webkit-box;margin:0px;padding:0px;-webkit-box-pack:center;}.kslider .ks_wt li{text-align:center;list-style:none;background: -webkit-linear-gradient(top, #AAAAAA 0%,#979797 100%);color: #fff;}.ks_wt_1 .ks_wt li{-webkit-box-flex:1;height:35px;line-height:35px;border-right:solid 1px #BBB;}.ks_wt_2 .ks_wt li{background:-webkit-linear-gradient(top, #e7e7e7 0%,#dfdfdf 100%);text-indent: 20px;height:10px;width:10px;overflow:hidden; border-radius:100%;margin:5px;}.ks_wt_1 .ks_wt .ks_t2{background:-webkit-linear-gradient(top, #e7e7e7 0%,#dfdfdf 100%); color:#000;}.ks_wt_2 .ks_wt .ks_t2{background: -webkit-linear-gradient(top, #AAAAAA 0%,#979797 100%); -webkit-animation:kt2 500ms linear;}@-webkit-keyframes kt2{    0%{-webkit-transform:scale(1);}    100%{-webkit-transform:scale(1.5);}}

js

/*    zepto.kslider.js    lxk 2014.08.14    www.cnblogs.com/wingkun*/ (function ($) {        /*        参数:config        change:tab页变更事件            参数e: 当前页码        tick:自动滚动间隔时间毫秒 (不设置则不自动滚动)        maxWidth:容器最大宽度 (默认有100%)        minWidth:容器最小宽度 (默认有100%)        className:样式类名            "ks_wt_1" 标题栏-方形 (默认)            "ks_wt_2" 标题栏-小圆形            或者你自定义的类名        */        $.fn.slider = function (config) {            config = $.extend({}, { className: "ks_wt_1" }, config);            var b = $(this), tw, timer,            target = b.find(".ks_dbox"),            title = b.find(".ks_wt"),            m = { initX: 0, initY: 0, startX: 0, endX: 0, startY: 0, canmove: false },            currentTab = 0;            b.toggleClass(config.className,true);            if (config.maxWidth) b.css({ maxWidth: config.maxWidth });            if (config.minWidth) b.css({ mixWidth: config.minWidth });            title.on("click", function (e) {                if (e.target == this) return;                toTab($(e.target).index());            });            b.on("touchstart", function (e) {                var et = e.touches[0];                if ($(et.target).closest(".ks_dbox").length != 0) {                    m.canmove = true, m.initX = m.startX = et.pageX;                    m.initY = et.pageY;                    clearTimer();                }            }).on("touchmove", function (e) {                var et = e.touches[0];                if (m.canmove && Math.abs(et.pageY - m.initY) / Math.abs(et.pageX - m.initX) < 0.6) {                    //             if (m.canmove && Math.abs(et.pageX - m.startX) > 10) {                    target.removeClass("ks_ts").css("-webkit-transform", "translate3d(" + (m.endX += et.pageX - m.startX) + "px,0,0)");                    m.startX = et.pageX;                    e.preventDefault();                }            }).on("touchend", function (e) {                if (!m.canmove) return;                target.toggleClass("ks_ts", true);                tw = target.width();                //是否超过了边界                var bl = false, current = Math.abs(m.endX / tw);                if (m.endX > 0) {                    current = m.endX = 0;                    bl = true;                }                else if (m.endX < -tw * (target.children().length - 1)) {                    current = target.children().length - 1;                    bl = true;                }                if (!bl) {                    if (m.endX % tw != 0) {                        //target.css("transform", "translate(" + (m.endX = -tw*Math.abs(Math.round(m.endX/tw))) + "px,0px)");                               var str = parseInt((current + "").split(".")[1][0]);                        if (e.changedTouches[0].pageX > m.initX) {                            //往右                            current = str <= 9 ? Math.floor(Math.abs(current)) : Math.abs(Math.round(m.endX / tw));                        } else {                            //往左                            current = str >= 1 ? Math.floor(Math.abs(current)) + 1 : Math.abs(Math.round(m.endX / tw));                        }                    }                }                toTab(current);                setTimer();                m.canmove = false;            });            var move = function (i) {                target.css("-webkit-transform", "translate3d(" + (m.endX = i) + "px,0,0)");            }            var setIndex = function (i) {                return i < 0 ? 0 : i >= target.children().length ? target.children().length - 1 : i;            }            var toTab = function (i) {                i = setIndex(i), tw = target.width();                move(-tw * i), toTitle(i);                if (currentTab != i && config.change) {                    config.change(i);                }                currentTab = i            }            var toTitle = function (i) {                if (title.length == 0) return;                title.children().toggleClass("ks_t2", false).eq(i).toggleClass("ks_t2", true);            }            var setTimer = function () {                if (!config.tick) return;                if (timer) clearTimer();                timer = setInterval(function () {                    toTab(currentTab >= target.children().length - 1 ? 0 : currentTab + 1);                }, config.tick)            }            var clearTimer = function () {                clearInterval(timer);                timer = null;            }            setTimer();            return {                add: function (t, c) {                    //添加tab                    title.append("<li>" + t + "</li>");                    target.append("<div class=\"ks_warp\">" + c + "</div>");                    return this;                },                remove: function (i) {                    //移除tab                    if (title.children().length == 1) return;                    i = setIndex(i);                    title.children().eq(i).remove();                    target.children().eq(i).remove();                    if (i == currentTab) toTab(0);                    return this;                }, tab: function (i) {                    //设置或者获取当前tab                    return i ? toTab(i) : currentTab;                }            }        }    })(Zepto);

Others

  • Only the basic zepto is quoted in the demo. In fact, its touch.js on the mobile side is also very necessary. After quoting You can replace the click in the code with zepto's tap event
  • Address: https://github.com/madrobby/zepto/blob/master/src/touch.js#files

  • Container Please pay attention to the box layout and internal html style used
  • Only supports most webkit kernel browsers
  • Testing needs to be carried out on mobile devices, chrome is required on the computer, F12 is opened, and in the console Next to it, camouflage the environment, as shown below:
  • Released in a hurry, please point out if there are any mistakes, demo download: here

    I have a lot of free time after work... I'm here to look for a part-time job!

    asp.net/js/jquery/html5/css3/Experienced in mobile front-end

    (Coordinates [Changsha], Industry [Lottery Industry] -- if necessary)

    Please support me!

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    atom中 40+ 个常用插件推荐分享(附插件安装方法)atom中 40+ 个常用插件推荐分享(附插件安装方法)Dec 20, 2021 pm 04:14 PM

    本篇文章给大家分享40+ 个atom常用插件,并附上在atom中安装插件的方法,希望对大家有所帮助!

    vscode插件分享: 6个Vue3开发必备插件vscode插件分享: 6个Vue3开发必备插件Dec 09, 2022 pm 08:36 PM

    本篇文章给大家整理分享 6 个 Vue3 开发必备的 VSCode 插件,可以直接用过 VSCode 的插件中心直接安装使用,希望对大家有所帮助!

    五个IntelliJ IDEA插件,高效编写代码五个IntelliJ IDEA插件,高效编写代码Jul 16, 2023 am 08:03 AM

    人工智能AI是当前广受认可的未来趋势和发展方向。虽然有些人担心AI可能会取代所有的工作,但实际上只会取代那些重复性高、产出低的工作。因此,我们应该学会更加聪明地工作,而不是使劲努力地工作。本文介绍5个由AI驱动的Intellij插件,这些插件可以帮助你提高生产力,减少繁琐的重复性工作,让你的工作更加高效、便捷。1GithubCopilotGithubCopilot是由OpenAI和GitHub联合开发的一款人工智能代码辅助工具。它使用了OpenAI的GPT模型来分析代码上下文,预测并生成新的代码

    用 VSCode 写 Python,这14个插件不容错过!用 VSCode 写 Python,这14个插件不容错过!May 24, 2023 pm 05:19 PM

    可以说,VisualStudioCode这个编辑器,让微软在开源社区赢回了王者段位,要知道全球2400万开发者中有1400万称VSCode为自己的家,再加上GitHub和VSCode的结合,几乎所有的程序员的都离不开VSCode,不过,VSCode如此优秀,值得每个程序员使用,甚至我觉得非程序员都可以用它来码字。如果你还没用过VSCode,那访问这里安装[1]一个吧,很可能就打开了一个新世界。今天分享14个非常实用VSCode插件,可以让你写代码如同神一般,尤其是

    2023年最新最全的VScode插件推荐2023年最新最全的VScode插件推荐Jan 24, 2023 am 05:30 AM

    这篇文章主要介绍了这么多年来我在使用 VSCode 过程中用到的一些不错的插件。这些VSCode插件,帮你打造地表最强IDE!

    【吐血总结】23个VSCode 插件,助你提高开发效率和美观性【吐血总结】23个VSCode 插件,助你提高开发效率和美观性Mar 10, 2022 pm 08:01 PM

    本篇文章给大家总结了23个各种功能的VSCode 插件,可以帮助开发者提高开发效率和美观性,希望对大家有所帮助!

    浏览器增强版ChatGPT无敌了?超强插件Monica,能聊能写效率Max浏览器增强版ChatGPT无敌了?超强插件Monica,能聊能写效率MaxMay 03, 2023 pm 06:58 PM

    提起Monica,你会想到什么?是老友记里的主角之一Monica·Geller,一个热心肠的女主人形象;还是心跳文学部里的疯疯癫癫的Monika?或者,最近爆火的Chrome插件——Monica。它的功能实在是太强大了,用完一次保你爱不释手。毕竟,搭载了ChatGPT的网页助手,能是俗物吗?Monica功能大赏首先明确一点,Monica是ChatGPT在网页上的应用,换句话说,Monica就是靠着ChatGPTAPI的强大功能才厉害。而仔细看看Chrome商店中的介绍,我们就会发现Monica真

    如虎添翼,六个让你效率翻倍的ChatGPT插件如虎添翼,六个让你效率翻倍的ChatGPT插件May 17, 2023 pm 02:28 PM

    ChatGPT是一个超强的AI应用程序,OpenAI已经发布的GPT-4引起了更广泛的关注。ChatGPT是由OpenAI开发的专门从事对话的AI聊天机器人,其主要目标是使AI系统更自然地进行互动。大家可能都已经尝试过ChatGPT,今天讲一讲与这个全新工具互动的不同方法。本文总结了6个可以使ChatGPT成为日常助手(甚至超越日常助手)的工具!1.【GoogleChromeExtension】在任何地方使用ChatGPT想在任何地方轻松地使用ChatGPT吗?那么你可以使用Chrome插件(h

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment