使用jquery通过动画删除图像

时间:2011-11-09 18:28:33

标签: jquery html image

我有许多动态创建的图像,我在空div中显示它们。 现在我有一个<span>标签,我在其中显示删除按钮以删除带有小褪色动画的图像。 现在,当我点击没有任何反应!我不太清楚我在做什么?

所以我需要你的帮助!

这是我的代码:

的CSS:

<style type="text/css">
#container 
{

border:dashed 7px #808080;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 25px;
    width: 360px;
    height: 285px;
    position:absolute;
    left:520px;
    top:120px;
    padding: 0 0 5px 0;


}
#container a 
{
    position:relative; 
    border: 1px solid blue;
    float: left;
    display: block;
    width: 64px; height: 64px;
    margin: 5px 0 0 5px
}
#container a:visited {
    border: 1px solid #90f
}
#container img {
    border: 0;
}

#container a span { display:none; background-image:url(images/delete.gif); background-repeat:no-repeat; width:16px; height:16px; position:absolute; right:0px; top:0px;} 
#container a:hover span { display:block;} 
</style>

脚本:

<script>
$("a span").click(function() {
    $("#container img").fadeOut("normal", function() {
        $(this).remove();
    });
});
</script>

展示图片的容器:

<div id="container">

</div>

这是我使用Plupload上传图片并显示:

的脚本
   <script type="text/javascript">
    $(function () {
        document.getElementById('Nextbutton').style.visibility = "hidden"; // show 
        $("#uploader").plupload({
            // General settings
            runtimes: 'gears,flash,silverlight,browserplus,html5',
            url: 'Test.aspx',
            max_file_size: '10mb',
            max_file_count: 20,
            chunk_size: '1mb',
            unique_names: true,

            filters: [
            { title: "Image files", extensions: "jpg,gif,png" },
            { title: "Zip files", extensions: "zip" }
        ],


            flash_swf_url: 'js/plupload.flash.swf',


            silverlight_xap_url: 'js/plupload.silverlight.xap'
        });



        $('form').submit(function (e) {
            var uploader = $('#uploader').plupload('getUploader');

            if (uploader.files.length > 0) {

                uploader.bind('StateChanged', function () {
                    if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                        $('form')[0].submit();
                    }
                });

                uploader.start();

            }
            else
            //alert('You must at least upload one file.');

                return false;
        });
        var uploader = $('#uploader').plupload('getUploader');

        uploader.bind('FilesAdded', function (up, files) {
            //              jQuery('#container a').html('');
            $('#container > *').remove();
            var i = 0;
            while (i++ < up.files.length) {
                var ii = i;
                while (ii < up.files.length) {
                    if (up.files[i - 1].name == up.files[ii].name) {
                        $.msgBox({
                            title: "Ooops",
                            content: "There is already an image with the same filename and cannot be added.",
                            type: "error",
                            showButtons: true,
                            opacity: 0.9,
                            autoClose: false
                        });
                        uploader.removeFile(up.files[ii]);
                    } else {
                        ii++;
                    }
                }
            }
            if (i > 20) {
                $.msgBox({
                    title: "Info",
                    content: "Uuh! Please don't put me any more files.<br>Maximum Upload limit is only 20 Images.<br>Rest of the Images will be removed.",
                    type: "info",
                    showButtons: true,
                    opacity: 0.9,
                    autoClose: false
                });
                $('#uploader_browse').hide();
            }
        });
        uploader.bind('FilesRemoved', function (up, files) {
            if (up.files.length < 20) {
                $('#uploader_browse').fadeIn("slow");
            }
        });


        uploader.bind('FileUploaded', function (up, file, res) {
            $('#container').append("<div class='container a'><a href='uploads/" + document.getElementById("currentDirectory").value + "/" + file.name + "' target='blank'><img src='uploads/" + document.getElementById("currentDirectory").value + "/" + file.name + "' width='64' height='64'/></a></div>");


            $("#container a").append("<span></span>");
            $("#container a").hover(function () {
                $(this).children("span").fadeIn(200);
            }, function () {
                $(this).children("span").fadeOut(600);
            });


            var $imageContainers = $('#container a');

            $imageContainers.each(function (index) {
                $(this).delay(index * 50).fadeTo(400, 0.5);
            });

            $imageContainers.mouseover(function () {
                $(this).css('opacity', 1);
                $(this).find('span.del').show();
            });
            $imageContainers.mouseout(function () {
                $(this).css('opacity', 0.5);
                $(this).find('span.del').hide();
            });


            if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                document.getElementById('Nextbutton').style.visibility = "visible"; // show 
                showStickySuccessToast();
            }
            uploader.removeFile(file);
        });

    });


     function randomString(length) {
         var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');

         if (!length) {
             length = Math.floor(Math.random() * chars.length);
         }

         var str = '';
         for (var i = 0; i < length; i++) {
             str += chars[Math.floor(Math.random() * chars.length)];
         }
         return str;
     }

    </script>

2 个答案:

答案 0 :(得分:1)

我没有使用你的脚本来构建图像,但我已经模拟了它,只是创建了结果html。以下是用淡化删除的脚本:

$(".container a span").click(function() {
    $(this).closest('div.container').fadeOut("normal", function() {
        $(this).remove();
    });
    return false;
});

此外,在您的脚本中使用要创建的项目的当前实例:

var $currentTile = $('#container').append("<div class='container a'><a href='uploads/" + document.getElementById("currentDirectory").value + "/" + file.name + "' target='blank'><img src='uploads/" + document.getElementById("currentDirectory").value + "/" + file.name + "' width='64' height='64'/></a></div>");
$currentTile .... // work with this instance

代码:http://jsfiddle.net/XBB6y/1/

答案 1 :(得分:1)

所以你需要使用.live方法来动态生成锚点,以便禁用它们的默认功能,就像我们在聊天中谈到的那样。

$(".container a").live('click', ...etc