javascript - 标题在滚动时淡入

标签 javascript jquery css

你好,当 window.scrollTop 大于标题位置时,我试图让每个标题都必须淡入,但我的代码不起作用,我得到了奇怪的 html 而不是我的标题。有什么想法吗?

$(window).scroll( function(){

    /* Check the location of each desired element */
    $('.inner h2').each( function(i){

        var bottom_of_object = $(this).position().top + $(this).outerHeight();
        var bottom_of_window = $(window).scrollTop() + $(window).height();

        /* If the object is completely visible in the window, fade it it */
        if( bottom_of_window > bottom_of_object ){

            var header = $(this);           
            var title = header.html();
            header.html('');        
            var arrayTitle = title.split('');
            var i = 0;              

            var interval = setInterval(function(){
            if (i > arrayTitle.length) 
            {
              header.html(title);        // wipe out the <span> tags
              clearInterval(interval);
            } 
            else
            {
              $('<span>')
                .html(arrayTitle[i])
                .appendTo(header)
                .hide()
                .fadeIn(50);                    
                i++;      
            }
            }, 50);

        }            
    }); 

});

http://jsfiddle.net/e5qaD/2005/

最佳答案

Solved Check Demo 1 Demo 2

我做了什么 更改了 span 的使用,而不是使用 var temp 通过 char 附加标题文本字符,每次将此 temp 作为内容传递到标题标签 innerhtml 作为 header。 html(临时)

稍微更改了代码,但确实有效

            var temp='';
            var interval = setInterval(function(){
            if (i > arrayTitle.length) 
            {
              header.html(title);        // wipe out the <span> tags
              clearInterval(interval);
            } 
            else
            { 

             if (i < arrayTitle.length) 
            {

                temp =temp+arrayTitle[i];
            } 


              //  alert(temp);
                header.html(temp).fadeIn(1000);

                 i++; 
            }
            }, 50);

关于javascript - 标题在滚动时淡入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24106042/

相关文章:

javascript - 如何在文本区域内添加标签?

javascript - 如何从模态提交 Bootstrap 表单

javascript - jQuery、JavaScript 自动完成概念,但不是

jquery - 如何获取on()上的:selected form <option> when using .?

html - 我想让两张图片并排 float

css - 谷歌字体未在 IE 或边缘加载

javascript - 如何将在扩展菜单中输入的数据发送到另一个文件

javascript - HTML 输入输出

javascript - 文档可离线搜索...Java、Javascript、JQuery、Angular...等。

php - 将值放在中心的文本框中