Blogger Information
Blog 61
fans 1
comment 0
visits 69825
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0521-jq制作相册
我的博客
Original
690 people have browsed it

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>作业相册</title>
    <style>
        .warp {
            width: 360px;
            height: auto;
            background-color: linen;
            border: 3px double grey;
            color: #363636;
            border-radius: 2%;
            box-shadow: 2px 2px 2px #888;
        }
        .warp .header {
            padding: 15px;
        }
        .warp .header h2 {
            text-align: center;
        }
        .add {
            width: 100px;
            height: 30px;
            border: none;
            cursor: pointer;
            background-color: deepskyblue;
            color: white;
        }
        .add:hover {
            background-color: orange;
            font-size: 1.1rem;
        }
        .main {
            overflow: hidden;
        }

        .main ul {
            padding: 0;
            margin: 0;
        }

        .main ul li {
            list-style-type: none;
            float: left;
            margin-left: 20px;
            margin-bottom: 10px;
            width: 150px;
            height: 200px;
            text-align: center;
        }

        .main ul li button {
            margin: 3px;
            border: none;
            border-radius: 5%;
            background-color: deepskyblue;
        }
        .main ul li button:hover {
            background-color: orange;
            color: white;
            cursor: pointer;
        }

    </style>


</head>
<body>
<div class="warp">
    <div class="header">
        <h2>私人相册</h2>
        <p>
            <label for="img_url">输入图片地址</label>
            <input type="file" name="img_url" id="img_url" placeholder="图片地址">
        </p>
        <p>
            图片类型:
            <input type="radio" id="rect" name="border" value="0" checked><label for="rect">直角</label>
            <input type="radio" id="radius" name="border" value="10%"><label for="radius">圆角</label>
            <input type="radio" id="circle" name="border" value="50%"><label for="circle">圆形</label>
        </p>
        <p>
            <label for="shadow">是否添加阴影:</label>
            <select name="shadow" id="shadow">
                <option value="false" selected>不添加</option>
                <option value="true">添加</option>
            </select>
        </p>
        <p><button class="add">添加图片</button></p>
    </div>
    <div class="main">
        <ul>
<!--                        <li>-->
<!--                            <img src="http://qq.io/js/0521/static/images/2.jpg" alt="" width="150">-->
<!--                            <button>前移</button>-->
<!--                            <button>后移</button>-->
<!--                            <button>删除</button>-->
<!--                        </li>-->
        </ul>
    </div>
</div>

<script src="static/js/jquery-3.4.1.js"></script>
<script>
    $(function () {
//1、先给按钮添加事件
        $('.add').on('click',function () {
            // var img_url=$('#img_url').val();  //这里获取了ID的值,后边就用img_url ,注销后直接用值
            //非空验证
            if($('#img_url').val().length===0){
                // confirm("请选择一张图片");
                alert('请选择一张图片');
                return false;  //返回假
            }


//2、制作样式
            //添加图片类型:直角、圆角、圆形
           var img_radio =  $('input[type="radio"]:checked').val();

           //是否添加阴影
            if ($(':selected').val() === 'true') {   //注意 ===后边的val值是字符串必须加'',如果是0和1代表也是字符串要加引号。
               var shadow = '10px 10px 10px #888';
            }else{shadow = 'none'}



//3、创建图片,并给图片附上以上样式

            //由于获取到的img_url地址是假地址,因此需要将他换成绝对地址,真实地址是:qq.io/js/0521/static/images/...
          //用数组分割方法 split(),根据\来分割,由于\是转义字符,所以用 ‘\\’来转换。
            console.log($('#img_url').val());  //前边已经转换var img_url=$('#img_url').val(); 109行
            var img_u = $('#img_url').val().split('\\')[2];
            console.log(img_u);
            img_url = 'http://qq.io/js/0521/static/images/'+ img_u;
            console.log(img_url);

         // 创建一个图片标签,用对象的方式给他们赋值

           var img = $('<img>')
                .attr({
                    src:img_url,
                    width:150,
                    height:150
                })
                .css({
                    //这里放圆角、阴影CSS
                    'border-radius':img_radio,
                    'box-shadow': shadow         //非法键名用 ''括起来

                });


//4、创建三个按钮
         //  三个按钮:前移(before)、后移(after)、删除(remove)

            var before = $('<button>').text('前移');
            var after = $('<button>').text('后移');
            var remove = $('<button>').text('删除');


//5、把图片和三个按钮放到同一个<li>标签下,并追加给 <ul>标签。
            //前移,先拿到需要移动图片的前一个图片,然后在他前边插入自己。按钮的前面什么都没有,所以必须先把他和图片放在同一个节点<li>下
            var li = $('<li>');
            li.append(img,before,after,remove).appendTo('ul');

//6、必须最后来做,给三个按钮添加事件 (涉及父标签的移动和删除,所以最后来做)

            before.on('click',function () {
                 //先获取他的父节点
                var fu = $(this).parent();
                var li = fu.prev();
                li.before(fu);
            });

            after.on('click',function () {
                var fu = $(this).parent();  //按钮的父节点为 <li>
                var li =fu.next();          //父节点的下一个节点 <li>
                li.after(fu);               //在下一个li之后插入fu
            });

            remove.on('click',function () {
                var fu = $(this).parent();  //按钮的父节点为 <li>
                fu.remove();
            })


        });



    })


</script>



</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post