使用fadeIn()添加html以淡入

append html to fade in using fadeIn()

本文关键字:html 淡入 添加 fadeIn 使用      更新时间:2023-09-26

我不能让附加到$('ul#posts')<li>元素淡出。

我尝试过的事情。

  1. 设置<li>display:none,然后渐入。附加的html在请求之后是不可见的。这意味着fadeIn()不起作用

  2. $(wall_post).appendTo('ul#posts').fadeIn("slow");似乎也不起作用

代码:

var wall_post = '<li> <img src="image/avatar.jpg" class="avatar"><div class="status"><h2><a href="#" target="_blank">disran</a></h2><p class="message">' + textarea_content + '</p> ' + image_html + '<div class="data"><p class="name"><a href="' + siteurl + '" target="_blank">' + sitetitle + '</a></p><p class="caption">' + siteurl + '</p><p class="description">' + sitedesc + '</p></div></div> </div><p class="likes">5 hours ago · 100 Likes </p></li>';
var message_wall = $('#message_wall').attr('value');
$.ajax({
    type: "GET",
    url: "insert.php",
    data: "message_wall=" + wall_post,
    success: function () {
        //$('ul#posts').append(wall_post).fadeIn('slow');
        //$(wall_post).appendTo('ul#posts').fadeIn("slow");
        $('ul#posts').append(wall_post).fadeIn('slow');
    }
});

<li>
    <img src="image/avatar.jpg" class="avatar">
    <div class="status">
        <h2><a href="#" target="_blank">disran</a></h2>
        <p class="message">[.. textarea_content ..]</p> 
        [.. image_html ..]
        <div class="data">
            <p class="name">
                <a href="[.. siteurl ..]" target="_blank">[.. sitetitle ..]</a>
            </p>
            <p class="caption">[.. siteurl ..]</p>
            <p class="description">[.. sitedesc ..]</p>
        </div>
    </div>
    <p class="likes">5 hours ago 100 Likes</p>
</li>

您现在使用字符串来处理,不总是工作。所以我要做的就是把它变成这样的对象:

var wall_post = $('<li>[.. rest of html structure ..]</li>');
var message_wall = $('#message_wall').attr('value');
$.ajax({
    type: "GET",
    url: "insert.php",
    // why are you sending the wallpost?? why getting you HTML from PHP?? 
    // but i think its the message_wall variable you ment?
    data: "message_wall=" + wall_post,
    success: function () {
        $('ul#posts').append(wall_post)
        wall_post.hide(); // instant hide the wall_post
        wall_post.fadeIn('slow'); // let is fadein slow
    }
});

在应用fadeIn操作之前,您必须使隐藏或显示:none的wall_post。使用此操作

$('ul#posts').append(wall_post).hide('fast').fadeIn('slow');

这里是jsfiddle链接http://jsfiddle.net/gagan/M22EQ/

更新:

这比以前更合适,jsfiddle link

$('ul#posts').append($(wall_post).hide().fadeIn('slow'));

查看注释以了解先前解决方案的问题。

@gansdeep答案是完美的,即

   $('ul#posts').append($(wall_post).hide().fadeIn('slow'));

一个已经可见的元素不会淡入。你必须先隐藏它

wall_post.hide();
$('ul#posts').append(wall_post).fadeIn('slow');

尝试:

$('ul#posts').append(wall_post.hide().fadeIn('slow'));