兼容所有浏览器的菱形

1.jpg

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>菱形裁剪</title>
<script type="text/javascript" charset="utf-8" src="js/raph.js"></script>
<style type="text/css" charset="utf-8">
#canvas {
background-color: #F4F4F4;
left: 0;
position: absolute;
top: 0;
}
#paper {
left: 0;
position: relative;
top: 0;
}
h2 {
text-align: center;
}
#vic1, #vic2, #vic3 {
position:absolute;
left:0;
top:0;
}
shape, span {
font-size: 16px;
font-family:'Microsoft YaHei';
color: #ffffff;
font-style: oblique;
display:block;
height:100%;
width:100%;
text-align:center;
background-color: red;
}
</style>
</head>

<body>
<div id="canvas">
<div id="paper">
<div id="vic1"></div>
<div id="vic2"></div>
<div id="vic3"></div>
<div id="vic4"></div>
<div id="vic5"></div>
<div id="vic6"></div>
<div id="vic7"></div>
<div id="vic8"></div>
<div id="vic9"></div>
<div id="vic10"></div>
<div id="vic11"></div>
<div id="vic12"></div>
<div id="vic13"></div>
<div id="vic14"></div>
<div id="vic15"></div>
<div id="vic16"></div>
<div id="vic17"></div>
<div id="vic18"></div>
<div id="vic19"></div>
<div id="vic20"></div>
<div id="vic21"></div>
</div>
</div>
<script type="text/javascript">
(function(Raphael,wh) {
var attr = {
fill: "#fff",
stroke: "#BCBCBC",
"stroke-width": 1
},
attr2 = {
fill: "#F8F8F8",
stroke: "#BBBBBB",
"stroke-width": 1
},
attr3 = {
fill: "#795755",
stroke: "#BBBBBB",
"stroke-width": 1,
"opacity": "0",
"cursor": "pointer"
},
attr4 = {
fill: "#fff",
font: "40px Georgia",
"opacity": "0",
'text-anchor': 'start'
},
x = 30/146*wh, //距离横坐标点 这里也可以根据角度而计算
y = 30/146*wh, //距离竖坐标点 这里也可以根据角度而计算
width = wh,
height = wh,
xiejiao = parseInt(width * Math.sqrt(2)), //对角线
plength = document.getElementById("paper").getElementsByTagName("div").length, //总个数
itemCount = 9, //每行显示的个数
row = plength % itemCount == 0 ? plength / itemCount : parseInt(plength / itemCount) + 1, //行数
R = Raphael("paper", xiejiao * (itemCount / 2 + 0.5), xiejiao * (row + 0.5)), //绘制画布宽度
aus = {}, //声明数组
pstrs = attr,
temp,
H1 = 0,
L1 = "",
key = "",
imagesL1 = 0,
imagesH1 = 0,
_index = 0;

for (var i = 0; i < row; i++) {
//console.log("第" + i + "行");
for (var j = 1; j <= itemCount; j++) {
_index = j + i * itemCount;
if (_index > plength) {
break;
}
//console.log(j);
if (j % 2 != 0) {
pstrs = attr;
L1 = i * xiejiao + "r45t-0";
H1 = j == 1 ? 0 : (j - 1) / 2 * xiejiao;
imagesH1 = (xiejiao - 40) / 2 + xiejiao * i;
imagesL1 = H1 + 30;

//console.log("奇数:" + j + ":" + H1 + " " + L1 + "图:" +imagesL1+ ":"+ imagesH1);
} else {
pstrs = attr2;
L1 = (i * xiejiao + xiejiao) + "r45t-" + width;
H1 = j / 2 * xiejiao;
imagesH1 = (0.5 * xiejiao + 30 - 40) * 2 + xiejiao * i;
imagesL1 = (j / 2 - 0.5) * xiejiao + 35;

//console.log("偶数:" + j + ":" + H1 + " " + L1+ "图:" +imagesL1+ ":"+ imagesH1);
}
//绘制最底层的形状
R.path(Raphael.format("M{0},{1}h{2}v{3}h{4}z", x, y, width, height, -width)).transform("t" + H1 + "," + L1 + ",0s1").attr(pstrs);
//绘制内容图片
R.image("images/big_1.jpg", imagesL1, imagesH1, 133, 40); //图片的位置及大小
//绘制遮罩层
temp = R.path(Raphael.format("M{0},{1}h{2}v{3}h{4}z", x, y, width, height, -width)).transform("t" + H1 + "," + L1 + ",0s1").attr(attr3);
//绘制遮罩层上面的说明文字
R.text(imagesL1 + 20, imagesH1 + 20, "60." + _index+"a").attr(attr4).data("i", _index+"a");

key = "vic" + _index;
aus[key] = temp;
}
}
for (var state in aus) {
aus[state].color = Raphael.getColor();
(function(st, state) {
st[0].style.cursor = "pointer";
//console.log(st.next[0].childNodes[0].innerHTML.length*parseInt(st.next[0].style.fontSize)/2);
st.next[0].onmouseover = function() {
st.animate({
"opacity": ".5"
});
st.next.animate({
"opacity": "1.0"
});
};
st.next[0].onmouseout = function() {
st.animate({
"opacity": "0"
});
st.next.animate({
"opacity": "0"
});
};
st[0].onmouseover = function() {
st.animate({
"opacity": ".5"
});
st.next.animate({
"opacity": "1.0"
});
};
st[0].onmouseout = function() {
st.animate({
"opacity": "0"
});
st.next.animate({
"opacity": "0"
});
};
})(aus[state], state);
};
})(Raphael,146);
</script>
</body>

</html>