JQuery特效动画淡入淡出 两个ul标签中li互相移动

发布时间:2019-08-11编辑:脚本学堂
本文介绍了jquery动画淡入淡出效果的一例代码,实现两个ul标签中li互相移动,有需要的朋友参考下。

例子,jquery特效代码,动画淡入淡出效果。
 

复制代码 代码示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>两个ul标签中的li互相移动_www.jb200.com</title>
    <style type="text/css">
        ul{list-style-type:none;float:left;margin-right:30px;background-color:Green;width:100px;height:100px;padding:0px;}
        li{margin-bottom:5px;background-color:Red;}
    </style>
    <script src="../js/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var myJson = [{ "id": "1", "Name": "刘德华", "Age": "52" },
                       { "id": "2", "Name": "文章", "Age": "26" },
                       { "id": "3", "Name": "孙红雷", "Age": "40" },
                       { "id": "4", "Name": "葛优", "Age": "58"}];


                       $(function () {
                           //动态添加Json数据到leftUL中
                           var $leftUL = $("#leftUL");
                           var $rightUL = $("#rightUL");
                           for (var i = 0; i < myJson.length; i++) {
                               $myLi = $("<li id='" + myJson[i].id + "'>" + myJson[i].Name +
                                "," + myJson[i].Age + "岁</li>");
                               $myLi.click(function () {
                                   if ($(this).parent().attr("id") == "leftUL") {        //通过判断父元素的ID来控制往哪个UL添加
                                       $(this).fadeOut(1000, function () {
                                           $(this).appendTo($rightUL);
                                           $(this).fadeIn(1000);
                                       });
                                   }
                                   else {
                                       $(this).fadeOut(1000, function () {
                                           $(this).appendTo($leftUL);
                                           $(this).fadeIn(1000);
                                       });
                                   }
                               });
                               $leftUL.append($myLi);
                           }
                       });
    </script>
</head>
<body>
    <ul id="leftUL">
    </ul>
    <ul id="rightUL">
    </ul>
</body>
</html>