html+css 实现图片右上角加删除叉、图片删除按钮

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre><pre name="code" class="html"><!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.divX
{
    z-index:200;
    -moz-border-radius:20px;
    -webkit-border-radius:20px;
    line-height:10px;
    text-align:center;
    font-weight:bold;
    cursor:pointer;
    font-size:10px;
    display: none;
}
</style>
</head>
  
<body>
    <form id="form" style="margin-top:20px;">
         
        <div id="img_div_1">
            <input type="file" id="file_input" οnchange="addFile(this);" style="display:none"  />
            <div style="position: relative;">
                <img id="file_img" src="add_img.png" width="75" οnclick="file_input.click();" height="65" />
            </div>
            <div class="divX" id="img_zindex_div_1" οnclick="del()">
                <img src="no.png" width="16" height="16" />
            </div>
        </div>
         
    </form>
</body>
<script src="jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript">
     
    function addFile(ths) {
        var objUrl = getObjectURL(ths.files[0]);
        var left = $('#file_img').position().left;
        var top = $('#file_img').position().top;
        $('#img_zindex_div_1').css({position: "absolute", left: left + 75, top: top + 10, display: "block" });
        $('#file_img').attr("src", objUrl);
    }
     
    function del() {
        alert("删除");
    }  
     
    function getObjectURL(file) {
        var url = null ;
        if (window.createObjectURL!=undefined) { // @ www.xuepai.net basic
            url = window.createObjectURL(file) ;
        } else if (window.URL!=undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file) ;
        } else if (window.webkitURL!=undefined) { // @ www.haoshilao.net
            url = window.webkitURL.createObjectURL(file) ;
        }
        return url ;
    }
</script>
</html>

 

补充。由于拿left都是0

换种方式获取

1
2
3
4
5
6
var objUrl = getObjectURL(ths.files[0]);
       var left = $('#file_img').offset().left;
       var top = $('#file_img').offset().top;
        
       // left为默认图的最左侧距离, 添加后的图片可根据 默认图的宽度 - 删除层图片的宽度(即:当前我默认图的宽度为75,删除层的图片宽度为16, left = 75 - 16, 位置就能到图片的最右侧了! top也是同样道理)
       $('#img_zindex_div_1').css({position: "absolute", left: left + 59, top: top - 5, display: "block" });

 

posted @ 2026-01-08 22:05  zxcva  阅读(11)  评论(0)    收藏  举报