首页 > Web开发 > 详细

基于jQuery的打字机函数

时间:2020-06-28 19:45:20      阅读:74      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="pic" style="display: none;">
        这是一段打字机效果的文字
    </div>

    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
        (function ($) {
            $.fn.typewriter = function () {
                this.each(function () {
                    var $ele = $(this),
                        str = $ele.html(),
                        progress = 0;
                    $ele.html(‘‘);
                    var timer = setInterval(function () {
                        var current = str.substr(progress, 1);
                        if (current == ‘<‘) {
                            progress = str.indexOf(‘>‘, progress) + 1;
                        } else {
                            progress++;
                        }
                        $ele.html(str.substring(0, progress) + (progress & 1 ? ‘_‘ : ‘‘));
                        if (progress >= str.length) {
                            clearInterval(timer);
                        }
                    }, 150);
                });
                return this;
            };
        })(jQuery);

        $("#pic").show().typewriter(50);
    </script>
</body>

</html>
技术分享图片

基于jQuery的打字机函数

原文:https://www.cnblogs.com/zk12138/p/13204404.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!