-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangleIndicate.js
188 lines (175 loc) · 5.12 KB
/
triangleIndicate.js
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
/**
* triangleIndicate v1 - add triangle indicater to a rectagle block
* 功能: 给矩形框添加指示性的三角箭头
* Copyright 2015, ZhangDefu - QQ:2240493958, China , http://www.euku.net/
* 版权所有: 张德福 易优中国
* Released under the MIT license - http://opensource.org/licenses/MIT
*/
Array.prototype.indexOf = function(e){
for(var i=0,j; j=this[i]; i++){
if(j==e){return i;}
}
return -1;
}
;(function($){
$.fn.triangle = function(options){
if(this.length == 0) return this;
// support mutltiple elements
if(this.length > 1){
this.each(function(){$(this).triangle(options)});
return this;
}
// set a reference to our triangle element
var elem = this;
var defaults = {
color:"white",//the backgrond-color of the triangle 三角形的颜色.
//white, blue, yellow, cyan, lime, purple, tomato
position:"top",//top, bottom, left, right
unitWidth:26,
unitHeight:13
};
var triangelClass = "triangel";
var targetClass = "indicate-target";
var colorAry;
var init = function(){
colorAry = new Array(7);
colorAry[0] = "white";
colorAry[1] = "lime";
colorAry[2] = "yellow";
colorAry[3] = "blue";
colorAry[4] = "tomato";
colorAry[5] = "cyan";
colorAry[6] = "purple";
if(!$(elem).hasClass(targetClass)){
$(elem).addClass(targetClass);
}
}
init();
var setting = $.extend({}, defaults, options);
//calculate width and height
//计算 width 和 height
var width = setting.unitWidth;
var height = setting.unitHeight;
var getSize = function(){//if succeeded ,return true; else ,return false. //有异常,则返回false. 计算成功,则返回tru
switch(setting.position){//top, bottom, left, right
case "top": case "bottom":
width = setting.unitWidth;
height = setting.unitHeight;
break;
case "left": case "right":
width = setting.unitHeight;
height = setting.unitWidth;
break;
default:
return false;
break;
}
return true;
}
//calcuate left and top
//计算 left 和 top
var left = 0;
var top = 0;
var getPosition = function(){//if succeeded ,return true; else ,return false. //有异常,则返回false. 计算成功,则返回true
var widthAll = $(elem).width();
var heightAll = $(elem).height();
switch(setting.position){//top, bottom, left, right
case "top":
left = (widthAll - setting.unitWidth) / 2;
top = 0 - setting.unitHeight;
break;
case "bottom":
left = (widthAll - setting.unitWidth) / 2;
top = heightAll;
break;
case "left":
left = 0 - setting.unitHeight;
top = (heightAll - setting.unitWidth) / 2;
break;
case "right":
left = widthAll;
top = (heightAll - setting.unitWidth) / 2;
break;
default:
return false;
break;
}
return true;
}
//calculate background-position
//计算 background-position
var backLeft = 0;
var backTop = 0;
var getBackgroundPosition = function(){//if succeeded ,return true; else ,return false.
//计算 background-position. 有异常,则返回false. 计算成功,则返回true
var colorIndex = colorAry.indexOf(setting.color);
if(colorIndex < 0) return false;
switch(setting.position){//top, bottom, left, right
case "top":
backTop = 0 - 2 * setting.unitWidth;
backLeft = 0 - colorIndex * setting.unitWidth;
break;
case "bottom":
backTop = 0 - (2 * setting.unitWidth + setting.unitHeight);
backLeft = 0 - colorIndex * setting.unitWidth;
break;
case "left":
backTop = 0;
backLeft = 0 -colorIndex * setting.unitHeight;
break;
case "right":
backTop = 0 - setting.unitWidth;
backLeft = 0 - colorIndex * setting.unitHeight;
break;
default:
return false;
break;
}
return true;
}
var addIndicator = function(){//if succeeded ,return true; else ,return false. //成功返回true, 失败返回false
var newElem = document.createElement("div");
$(newElem).addClass(triangelClass);
var isSizeOk = getSize();
if(!isSizeOk) return false;
var isBackOk = getBackgroundPosition();
if(!isBackOk){
return false;
}
var isPositionOk = getPosition();
if(!isPositionOk){
return false;
}
$(newElem).css("position", "absolute")
.css("width", width + "px")
.css("height", height + "px")
.css("top", top + "px")
.css("left", left + "px")
.css("background-position", backLeft + "px " + backTop + "px");
$(elem).append(newElem);
return true;
}
addIndicator();
/**
* Window resize event callback
*/
var resizeWindow = function(){
//elem = this;
var indicatorElem = $(elem).children("." + triangelClass);
var isPositionOk = getPosition();
if(!isPositionOk){
return false;
}
$(indicatorElem).css("top", top + "px")
.css("left", left + "px");
}
$(window).resize(function(){
resizeWindow();
});
/**
* ===================================================================================
* = PUBLIC FUNCTIONS
* ===================================================================================
*/
}
})(jQuery);