I want to scroll the page vertically without using the scrollbar. instead i want to use 2 image tags for scrolling the page. this was the code i tried for the scroll but it didnt look good:
JavaScript
x
39
39
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
2
<style type="text/css">
3
div.mousescroll {
4
overflow: hidden;
5
}
6
7
div.mousescroll:hover {
8
overflow-y: scroll;
9
}
10
</style>
11
12
<script language="javascript" type="text/javascript">
13
$(function() {
14
$(".wrapper1").scroll(function left() {
15
$(".wrapper2")
16
.scrollLeft($(".wrapper1").scrollLeft());
17
});
18
$(".wrapper2").scroll(function right() {
19
$(".wrapper1")
20
.scrollLeft($(".wrapper2").scrollLeft());
21
});
22
});
23
</script>
24
25
<div id="first" class="wrapper1 ">
26
27
<div id="first2" class=" div1 ">
28
</div>
29
<br />
30
</div>
31
<br />
32
<div id="second" class="wrapper2 mousescroll">
33
<div id="second2" style=" overflow-x:scroll" class="div2">
34
<table>
35
36
37
</table>
38
</div>
39
</div>
imagine that the width of this table is 2000px and instead of scrollbar i wanna use two image tags that can do the scroll job. can anyone help me with this?
Advertisement
Answer
JavaScript
1
255
255
1
$(window).load(function () {
2
(function ($) {
3
jQuery.fn.extend({
4
slimScroll: function (o) {
5
var ops = o;
6
//do it for every element that matches selector
7
this.each(function () {
8
var isOverPanel,
9
isOverBar,
10
isDragg,
11
queueHide,
12
barHeight,
13
divS = "<div></div>",
14
minBarHeight = 30,
15
wheelStep = 30,
16
o = ops || {},
17
cwidth = o.width || "auto",
18
cheight = o.height || "250px",
19
size = o.size || "7px",
20
color = o.color || "#000",
21
position = o.position || "right",
22
opacity = o.opacity || 0.4,
23
alwaysVisible = o.alwaysVisible === true;
24
25
//used in event handlers and for better minification
26
var me = $(this);
27
28
//wrap content
29
var wrapper = $(divS)
30
.css({
31
position: "relative",
32
overflow: "hidden",
33
width: cwidth,
34
height: cheight,
35
})
36
.attr({ class: "slimScrollDiv" });
37
38
//update style for the div
39
me.css({
40
overflow: "hidden",
41
width: cwidth,
42
height: cheight,
43
});
44
45
//create scrollbar rail
46
var rail = $(divS).css({
47
width: "15px",
48
height: "100%",
49
position: "absolute",
50
top: 0,
51
});
52
53
//create scrollbar
54
var bar = $(divS)
55
.attr({
56
class: "slimScrollBar ",
57
style: "border-radius: " + size,
58
})
59
.css({
60
background: color,
61
width: size,
62
position: "absolute",
63
top: 0,
64
opacity: opacity,
65
display: alwaysVisible ? "block" : "none",
66
BorderRadius: size,
67
MozBorderRadius: size,
68
WebkitBorderRadius: size,
69
zIndex: 99,
70
});
71
72
//set position
73
var posCss = position == "right" ? { right: "1px" } : { left: "1px" };
74
rail.css(posCss);
75
bar.css(posCss);
76
77
//wrap it
78
me.wrap(wrapper);
79
80
//append to parent div
81
me.parent().append(bar);
82
me.parent().append(rail);
83
84
//make it draggable
85
bar.draggable({
86
axis: "y",
87
containment: "parent",
88
start: function () {
89
isDragg = true;
90
},
91
stop: function () {
92
isDragg = false;
93
hideBar();
94
},
95
drag: function (e) {
96
//scroll content
97
scrollContent(0, $(this).position().top, false);
98
},
99
});
100
101
//on rail over
102
rail.hover(
103
function () {
104
showBar();
105
},
106
function () {
107
hideBar();
108
}
109
);
110
111
//on bar over
112
bar.hover(
113
function () {
114
isOverBar = true;
115
},
116
function () {
117
isOverBar = false;
118
}
119
);
120
121
//show on parent mouseover
122
me.hover(
123
function () {
124
isOverPanel = true;
125
showBar();
126
hideBar();
127
},
128
function () {
129
isOverPanel = false;
130
hideBar();
131
}
132
);
133
134
var _onWheel = function (e) {
135
//use mouse wheel only when mouse is over
136
if (!isOverPanel) {
137
return;
138
}
139
140
var e = e || window.event;
141
142
var delta = 0;
143
if (e.wheelDelta) {
144
delta = -e.wheelDelta / 120;
145
}
146
if (e.detail) {
147
delta = e.detail / 3;
148
}
149
150
//scroll content
151
scrollContent(0, delta, true);
152
153
//stop window scroll
154
if (e.preventDefault) {
155
e.preventDefault();
156
}
157
e.returnValue = false;
158
};
159
160
var scrollContent = function (x, y, isWheel) {
161
var delta = y;
162
163
if (isWheel) {
164
//move bar with mouse wheel
165
delta = bar.position().top + y * wheelStep;
166
167
//move bar, make sure it doesn't go out
168
delta = Math.max(delta, 0);
169
var maxTop = me.outerHeight() - bar.outerHeight();
170
delta = Math.min(delta, maxTop);
171
172
//scroll the scrollbar
173
bar.css({ top: delta + "px" });
174
}
175
176
//calculate actual scroll amount
177
percentScroll =
178
parseInt(bar.position().top) /
179
(me.outerHeight() - bar.outerHeight());
180
delta = percentScroll * (me[0].scrollHeight - me.outerHeight());
181
182
//scroll content
183
me.scrollTop(delta);
184
185
//ensure bar is visible
186
showBar();
187
};
188
189
var attachWheel = function () {
190
if (window.addEventListener) {
191
this.addEventListener("DOMMouseScroll", _onWheel, false);
192
this.addEventListener("mousewheel", _onWheel, false);
193
} else {
194
document.attachEvent("onmousewheel", _onWheel);
195
}
196
};
197
198
//attach scroll events
199
attachWheel();
200
201
var getBarHeight = function () {
202
//calculate scrollbar height and make sure it is not too small
203
barHeight = Math.max(
204
(me.outerHeight() / me[0].scrollHeight) * me.outerHeight(),
205
minBarHeight
206
);
207
bar.css({ height: barHeight + "px" });
208
};
209
210
//set up initial height
211
getBarHeight();
212
213
var showBar = function () {
214
//recalculate bar height
215
getBarHeight();
216
clearTimeout(queueHide);
217
218
//show only when required
219
if (barHeight >= me.outerHeight()) {
220
return;
221
}
222
bar.fadeIn("fast");
223
};
224
225
var hideBar = function () {
226
//only hide when options allow it
227
if (!alwaysVisible) {
228
queueHide = setTimeout(function () {
229
if (!isOverBar && !isDragg) {
230
bar.fadeOut("slow");
231
}
232
}, 1000);
233
}
234
};
235
});
236
237
//maintain chainability
238
return this;
239
},
240
});
241
242
jQuery.fn.extend({
243
slimscroll: jQuery.fn.slimScroll,
244
});
245
})(jQuery);
246
247
//invalid name call
248
$("#Id").slimscroll({
249
color: "#aaa",
250
size: "6px",
251
width: "392px",
252
height: "525px",
253
});
254
});
255