-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwill_hover.js
86 lines (77 loc) · 2.97 KB
/
will_hover.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
/*****************************************************************************
* Are you tired of writing onmouseover= and onmouseout= for hover states?
*
* How to use: Include Prototype or jQuery and this file. Add "will_hover"
* class to any IMG tag that you want to have a hover state. Name hover state
* graphics with "-hover" at the end of the file name. Like "about.png" and
* "about-hover.png".
*
* New bonus feature! Add "will_hover_bg" class to any element with a CSS
* background image that you want to have a hover state. Name the graphics
* following the convention shown above.
*
* Created By: Jesse C Scott (jesse.c.scott AT gmail.com)
* Released into the public domain, no copyright is claimed on this work.
*
*****************************************************************************/
if ( typeof Prototype !== 'undefined' ) {
document.observe("dom:loaded", function() {
$$('.will_hover').each(function(el, index) {
Event.observe(el, 'mouseover', function() {
var ext = this.src.split('.').reverse().first();
this.src = this.src.replace('.' + ext, '-hover.' + ext);
});
Event.observe(el, 'mouseout', function() {
var ext = this.src.split('.').reverse().first();
this.src = this.src.replace('-hover.' + ext, '.' + ext);
});
});
$$('.will_hover_bg').each(function(el, index) {
Event.observe(el, 'mouseover', function() {
var ext = this.getStyle('background-image').replace(/^url\(/, '').replace(/\)$/, '').split('.').reverse().first();
this.setStyle({
backgroundImage: this.getStyle('background-image').replace('.' + ext, '-hover.' + ext)
});
});
Event.observe(el, 'mouseout', function() {
var ext = this.getStyle('background-image').replace(/^url\(/, '').replace(/\)$/, '').split('.').reverse().first();
this.setStyle({
backgroundImage: this.getStyle('background-image').replace('-hover.' + ext, '.' + ext)
});
});
});
});
} else if ( typeof jQuery !== 'undefined' ) {
$(document).ready(function() {
$('.will_hover').bind({
mouseover: function() {
var ext = this.src.split('.').reverse();
ext = ext[0];
this.src = this.src.replace('.' + ext, '-hover.' + ext);
},
mouseout: function() {
var ext = this.src.split('.').reverse();
ext = ext[0];
this.src = this.src.replace('-hover.' + ext, '.' + ext);
}
});
$('.will_hover_bg').bind({
mouseover: function() {
var ext = $(this).css('background-image').replace(/^url\(/, '').replace(/\)$/, '').split('.').reverse();
ext = ext[0];
$(this).css({
'background-image': $(this).css('background-image').replace('.' + ext, '-hover.' + ext)
});
},
mouseout: function() {
var ext = $(this).css('background-image').replace(/^url\(/, '').replace(/\)$/, '').split('.').reverse();
ext = ext[0];
$(this).css({
'background-image': $(this).css('background-image').replace('-hover.' + ext, '.' + ext)
});
}
});
});
} else {
alert('will_hover.js requires Prototype or jQuery.');
}