1.实战: 武林高手在线相册:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>在线相册管理</title> </head> <body> <style> .warp { width: 400px; height: auto; background-color: #efefef; border: 3px double gray; <!-- double 双实线 --> color: #363636; border-radius: 2%; } .warp .header { padding: 15px; } .warp .header h2 { text-align: center; } .add { width: 100px; height: 30px; border: none; cursor: pointer; background-color: skyblue; color: white; } .add:hover { background-color: orange; font-size: 0.95rem; } .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: lightgreen; } .main ul li button:hover { background-color: orange; color: white; cursor: pointer; } </style> <div class="warp"> <div class="header"> <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"><label for="rect">直角</label> <input type="radio" id="radius" name="border" value="10%"><label for="rect">圆角</label> <input type="radio" id="circle" name="border" value="50%"><label for="rect">圆形</label> </p> <p>是否添加阴影: <select name="shadow" id=""> <option value="0">不添加</option> <option value="1">添加</option> </select> </p> <p><button class="add">添加图片</button></p> <div class="main"> <ul> </ul> </div> </div> </div> <script src="jquery-3.3.1.js"></script> <script> $('button.add').on('click',function () { //获取图片相关信息 img_url = $('#img_url').val(); if (img_url.length === 0) { alert('请选择一张图片'); $('#img_url').focus(); return false; } //获取图片基本特征 //获取图片外观 img_url = 'http://php.io/0918/images/'+ img_url.split('\\')[2]; let img_type = $(':radio:checked').val(); console.log(img_type); //是否添加阴影 let shadow = 'none'; if ($(':selected').val() === '1') { shadow = '3px 3px 3px #666'; } //2. 创建图片并添加到页面中 let img = $('<img>') .prop('src',img_url) .width(150) .height(150) .css({ 'border-radius': img_type, 'box-shadow' : shadow }); let before = $('<button></button>').text('前移'); let after = $('<button></button>').text('后移'); let remove = $('<button></button>').text('删除'); let contaier = $('<li>'); contaier.append(img,before,after,remove); contaier.appendTo('ul'); //为3个操作添加功能 //前移功能 before.click(function () { let current = $(this).parent(); let prev = current.prev(); prev.before(current); }) //后移功能 after.click(function () { let current = $(this).parent(); let next = current.next(); next.after(current); }) //删除功能 remove.click(function () { if (confirm('确认删除吗?')) { $(this).parent().remove(); } return fasle; }) }) </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例