weavertour.js
7.61 KB
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
/**
* Created by lsj on 2015/3/31.
*/
var WeaverTour = (function(win,doc,$){
var TRIANGLE_EDGE = 10;
var tbody = $(doc.body);
var WeaverTour = win.WeaverTour = function(config){
this.options = $.extend({steps:[],speed:700},{steps:config});
this.currentstepNum = 0;
this.currentStep = null;
}
//开始导航
WeaverTour.prototype.startTour = function(){
if(tbody.length === 0){
tbody = $(doc.body);
}
var that = this;
setTimeout(function(){
if( that.options.steps.length > 0){
that.currentStep = that.options.steps[0];
that.generatorTipDialog();
that.setElOverLay();
}
},2000);
}
//下一步
WeaverTour.prototype.nextStep = function(){
this.currentStep.dialog.fadeOut("slow");
this.currentstepNum++;
this.currentStep = this.options.steps[this.currentstepNum];
if(this.currentStep.dialog){
this.currentStep.dialog.fadeIn('slow');
}else{
this.generatorTipDialog();
}
tbody.animate({scrollTop:this.currentStep.el.offset().top},this.options.speed);
this.setElOverLay();
}
//上一步
WeaverTour.prototype.preStep = function(){
this.currentStep.dialog.fadeOut("slow");
this.currentstepNum--;
this.currentStep = this.options.steps[this.currentstepNum];
if(this.currentStep.dialog){
this.currentStep.dialog.fadeIn('slow');
}else{
this.generatorTipDialog();
}
tbody.animate({scrollTop:this.currentStep.el.offset().top},this.options.speed);
this.setElOverLay();
}
//停止导航
WeaverTour.prototype.stopTour = function(){
var steps = this.options.steps;
//移除dialog
for(var i=0; i<steps.length; i++){
if(steps[i].dialog){
steps[i].dialog.remove();
}
}
//移除遮罩
$(".weavertour_overlay").remove();
this.options.steps = null;
}
//生成提示对话框
WeaverTour.prototype.generatorTipDialog = function(){
var $el = $("#"+this.currentStep.itemid),
showposition = this.currentStep.position || "bottom", dialog;
this.currentStep.el = $el;
//对话框
dialog = $("<div class='weavertour_dialog'> "+
"<div class='weavertour_arrow_"+showposition+"'>"+
"</div> "+
"<div class='weavertour_content'> "+
"<div class='weavertour_title'> "+
this.currentStep.title+
"</div> "+
"<div class='weavertour_info'> "+
this.currentStep.describe+
"</div>" +
"<div class='weavertour_toobar'>"+
"</div>" +
"</div>"+
"</div>");
this.currentStep.dialog = dialog;
tbody.append(dialog);
this.setDialogPageBtn();
this.setDialogPosition($el,showposition,dialog);
//绑定按钮事件
this.bindEvents();
}
//设置高亮
WeaverTour.prototype.setElOverLay = function(){
var offset = this.currentStep.el.offset(), width = this.currentStep.el.width(), height = this.currentStep.el.height(), cssitem = {};
if($(".weavertour_overlay").length > 0){
cssitem["width"] = width+'px';
cssitem["height"] = height+'px';
cssitem["left"] = offset.left+'px';
cssitem["top"] = offset.top+'px';
$(".weavertour_overlay").animate(cssitem,this.options.speed);
}else{
var overlay = $("<div class='weavertour_overlay'></div>");
overlay.css("width",width+'px').css("height",height+'px').css("left",offset.left+"px").css("top",offset.top+"px");
tbody.append(overlay);
}
}
//生成对话框
WeaverTour.prototype.setDialogPosition = function($el,position,dialog){
var $el_w = $el.width(), $el_h = $el.height(), d_w = dialog.width(), d_h = dialog.height(), cssitem={}, offset = $el.offset();
switch(position){
case "top":(function(){
if(d_w>=$el_w){
cssitem["left"] = (offset.left - (d_w/2 - $el_w/2))+'px';
}else{
cssitem["left"] = (offset.left + ($el_w/2 - d_w/2))+'px';
}
cssitem["top"] = (offset.top - d_h - TRIANGLE_EDGE)+'px';
})();break;
case "bottom":(function(){
if(d_w>=$el_w){
cssitem["left"] = (offset.left - (d_w/2 - $el_w/2))+'px';
}else{
cssitem["left"] = (offset.left + ($el_w/2 - d_w/2))+'px';
}
cssitem["top"] = (offset.top + $el_h + TRIANGLE_EDGE)+'px';
})();break;
case "left":(function(){
if(d_h>=$el_h){
cssitem["top"] = (offset.top - (d_h/2 - $el_h/2))+'px';
}else{
cssitem["top"] = (offset.top + ($el_h/2 - d_h/2))+'px';
}
cssitem["left"] = (offset.left - TRIANGLE_EDGE - d_w )+'px';
})();break;
case "right":(function(){
if(d_h>=$el_h){
cssitem["top"] = (offset.top - (d_h/2 - $el_h/2))+'px';
}else{
cssitem["top"] = (offset.top + ($el_h/2 - d_h/2))+'px';
}
cssitem["left"] = (offset.left + TRIANGLE_EDGE + $el_w )+'px';
})();break;
}
//设置tip样式
dialog.css(cssitem);
dialog.fadeIn('slow');
}
//生成分页按钮
WeaverTour.prototype.setDialogPageBtn = function(){
if(this.currentstepNum === (this.options.steps.length-1) && this.options.steps.length>1 ){
this.currentStep.dialog.find(".weavertour_toobar").html("<span class='weavertour_btn weavertour_quit'>退出</span><span class='weavertour_btn weavertour_pre'>上一步</span>");
} else if(this.currentstepNum >0 && (this.currentstepNum+1) < this.options.steps.length){
this.currentStep.dialog.find(".weavertour_toobar").html("<span class='weavertour_btn weavertour_quit'>退出</span><span class='weavertour_btn weavertour_next'>下一步</span><span class='weavertour_btn weavertour_pre'>上一步</span>");
}else if(this.currentstepNum === 0 && this.options.steps.length>1){
this.currentStep.dialog.find(".weavertour_toobar").html("<span class='weavertour_btn weavertour_quit'>退出</span><span class='weavertour_btn weavertour_next'>下一步</span>");
}else {
this.currentStep.dialog.find(".weavertour_toobar").html("<span class='weavertour_btn weavertour_quit'>退出</span>");
}
}
//绑定事件
WeaverTour.prototype.bindEvents = function(){
var that = this;
this.currentStep.dialog.delegate('.weavertour_btn','click',function(){
var current = $(this);
if(current.hasClass("weavertour_quit")){
//停止导航
that.stopTour();
}else if(current.hasClass("weavertour_next")){
//下一步
that.nextStep();
}else if(current.hasClass("weavertour_pre")){
//上一步
that.preStep();
}
});
}
return WeaverTour;
})(window,document,jQuery)