字的Jquery字淡入效果 [英] Jquery word for word fade in effect

查看:120
本文介绍了字的Jquery字淡入效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在寻找这个确切的脚本一段时间,我不能得到它的工作。我想使用从左到右一个字逐渐淡入效果。

ive been looking in about for this exact script for a while and i cant get it to work. Im looking to use a fade in effect from left to right word by word.

例如

 <div class="box5">
      <h1>Lorem ipsum dolor sit amet, ne mel vero impetus voluptatibus
      </h1>                 
</div>

我想让它淡入,然后足够的行逐渐淡入一个字,稍后使用延迟。

I want this to fade in then enough line to fade in word by word slightly later using a delay.

我当前的工作淡出,但它的完整容器看起来像这样

My current fade in works but it does it by the full container it looks like this

.reveal {
    position: relative;
    overflow: hidden;
}

/*initial - hidden*/
.reveal .reveal__cover {
    position: absolute;
    top: 0;
    left: -250px;
    height: 100%;
    margin: 2px;
    width: calc(100% + 250px);
}

.reveal .reveal__cover.reveal__uncovered {
    position: absolute;
    top: 0;
    left: 100%;
    height: 100%;
    width: calc(100% + 250px);
    margin: 2px;
    transition: left 2500ms ease-out 0ms;
}

.reveal__cover-section {
    height: 100%;
    float: right;
    /* NOTE: Background must match existing background */
    /*background: #000;*/
    background: #fff;
    width: 2%;
}

.reveal__10 {
    opacity: 0.1;
}

.reveal__20 {
    opacity: 0.2;
}

.reveal__30 {
    opacity: 0.3;
}

.reveal__40 {
    opacity: 0.4;
}

.reveal__50 {
    opacity: 0.5;
}

.reveal__60 {
    opacity: 0.6;
}

.reveal__70 {
    opacity: 0.7;
}

.reveal__80 {
    opacity: 0.8;
}

.reveal__90 {
    opacity: 0.9;
}

.reveal__100 {
    opacity: 1;
    width: 82%;
}

然后JS

function replaceAllInstances(source, search, replacement) {
    var regex = new RegExp(search, "g");
    var result = source.replace(regex, replacement);
    return result;
}

$.fn.isOnScreen = function (x, y) {

    if (x == null || typeof x == 'undefined')
        x = 1;
    if (y == null || typeof y == 'undefined')
        y = 1;

    var win = $(window);

    var viewport = {
        top: win.scrollTop(),
        left: win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();

    var height = this.outerHeight();
    var width = this.outerWidth();

    if (!width || !height) {
        return false;
    }

    var bounds = this.offset();
    bounds.right = bounds.left + width;
    bounds.bottom = bounds.top + height;

    var visible = (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));

    if (!visible) {
        return false;
    }

    var deltas = {
        top: Math.min(1, (bounds.bottom - viewport.top) / height),
        bottom: Math.min(1, (viewport.bottom - bounds.top) / height),
        left: Math.min(1, (bounds.right - viewport.left) / width),
        right: Math.min(1, (viewport.right - bounds.left) / width)
    };

    return (deltas.left * deltas.right) >= x && (deltas.top * deltas.bottom) >= y;

};

/*
 * Init specified element so it can be gradually revealed.
 *
 * Limitations:
 *  Only works on backgrounds with a solid color.
 *
 *  @param options = {
 *       id:'box3'
 *      ,background='#ffffff' //default
 *      ,delay='0' //default
 *  }
 *
 */
$.fn.initReveal = function (options) {
    console.log('-------------');
    console.log('selector:' + this.selector);
    var parent = $(this).parent();

    //grab a copy of the contents, then remove it from DOM
    var contents = $(this).clone();
    $(this).empty();

    var revealHtmlBlock = "<div class='reveal'> <div class='text reveal__inner reveal__inner-{class}'> </div> <div class='reveal__cover reveal__cover-{class}'> <div class='reveal__cover-section reveal__100'></div> <div class='reveal__cover-section reveal__90'></div> <div class='reveal__cover-section reveal__80'></div> <div class='reveal__cover-section reveal__70'></div> <div class='reveal__cover-section reveal__60'></div> <div class='reveal__cover-section reveal__50'></div> <div class='reveal__cover-section reveal__40'></div> <div class='reveal__cover-section reveal__30'></div> <div class='reveal__cover-section reveal__20'></div> <div class='reveal__cover-section reveal__10'></div> </div> </div>";
    revealHtmlBlock = replaceAllInstances(revealHtmlBlock, "{class}", options.id);

    $(revealHtmlBlock).appendTo(parent);
    contents.appendTo($('.reveal__inner-' + options.id));

    //handle options

    //delay
    if (options.delay === undefined) {
        console.log('delay set to 0');
        options.delay = 0; //set default
    } else {
        console.log('delay set to:' + options.delay);
    }
    var revealElementFunction = function (options) {
        $(this).performReveal(options);
    };

    //background
    if (options.background !== undefined) {
        $('.reveal__cover-' + options.id + ' .reveal__cover-section').css({'background-color': options.background});
    }

    //trigger the reveal at the specified time, unless auto is present and set to false
    if (options.auto === undefined || (options.auto !== undefined && options.auto)) {
        setTimeout(function () {
            console.log('call');
            revealElementFunction(options);
        }, options.delay);
    }

    //trigger on-visible
    if (options.trigger !== undefined) {
        var revealOnScreenIntervalIdMap = {};
        function uncoverText() {
            var onscreen = $('.reveal__inner-box4').isOnScreen();
            if ($('.reveal__inner-' + options.id).isOnScreen()) {
                $('.reveal__cover-' + options.id).addClass('reveal__uncovered');
                revealOnScreenIntervalIdMap[options.id] = window.clearInterval(revealOnScreenIntervalIdMap[options.id]);
            }
        }
        function showTextWhenVisible() {
            revealOnScreenIntervalIdMap[options.id] = setInterval(uncoverText, 800);
        }
        showTextWhenVisible();
    }

};

//--------------------
/*
 * trigger options:
 * immediately (on page load)
 * on event, eg. onclick
 * on becoming visible, after it scrolls into view, or is displayed after bein ghidden
 *
 *  @param options = {
 *       id:'box3'
 *  }
 *
 */
$.fn.performReveal = function (options) {
    var _performReveal = function () {
        $('.reveal__cover-' + options.id).addClass('reveal__uncovered');
    };
    //allow time for init code to complete
    setTimeout(_performReveal, 250);
};

主JS

jQuery(function () {
    //Box 1: reveal immediately - on page load
    //NOTE: id does refer to an element id, It is used to
    //      uniquely refer to the element to be revealed.
    var options1 = {
        id: 'box1',

        background: '#008d35'
    };
    $('.box1').initReveal(options1);

    //------------------------
    //Box 2: reveal after specified delay
    var options2 = {
        id: 'box2'
        , delay: 3000
        , background: '#008d35'
    };
    $('.box2').initReveal(options2);

    //------------------------
    //Box 3: reveal on event - eg. onclick
    var options3 = {
        id: 'box3'
        , auto: false
    };
    var box3 = $('.box3');
    box3.initReveal(options3);

    $('.btn-reveal').on('click', function () {
        box3.performReveal(options3);
    });

    //------------------------
    //Box 4: Reveal when element scrolls into the viewport
    var options4 = {
        id: 'box4'
        , auto: false
        , trigger: 'on-visible'
    };
    $('.box4').initReveal(options4);
});

//------------------------
//Box 5 reveal
    var options5 = {
        id: 'box5'
        , delay: 2500 ,
        background: '#008d35'
    };
    $('.box5').initReveal(options5);

有没有人知道如何使它逐字工作,而不是一行一行

does anyone have any idea how to make it work word by word and not line by line

推荐答案

这里有一个简单的方法可以建立。它会根据您设置的间隔值创建所需的跨度并将其淡化。

Here's a simple approach that you can build on. It creates the needed spans and fades them in based on interval value you set.

var fadeInterval = 300

$('h1').html(function(_, txt){
    var words= $.trim(txt).split(' ');
    return  '<span>' + words.join('</span> <span>') + '</span>';
}).find('span').each(function(idx, elem){        
    $(elem).delay(idx * fadeInterval).fadeIn();
});

DEMO

这篇关于字的Jquery字淡入效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆