-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathjquery.flip-quote.js
138 lines (114 loc) · 3.6 KB
/
jquery.flip-quote.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
/*
* jquery.flip-quote.js v1.0.1, jQuery Flip-Quote
*
* Copyright 2014 Mark Serbol.
* Use, reproduction, distribution, and modification of this code is subject to the terms and
* conditions of the MIT license, available at http://www.opensource.org/licenses/MIT.
*
* jQuery Flip-Quote creates a pull-quote from a text quote found in the document
* and flips to reveal the quote once it's scrolled into view.
* It also has a click feature that scrolls into and highlights the quote origin on the document.
*
* https://github.com/markserbol/flip-quote
*
*/
;(function($){
var defaults = {
quoteSelector:'q',
container:'.container',
quoteMarks:'"“" "”"',
bgColor:'#0080C0',
fontColor:'#FFF',
fontSize:'24px',
flipDuration:'0.7s',
pads:20
};
$.fn.flipQuote = function(options){
var opts = $.extend({}, defaults, options);
return this.each(function(){
var el = $(this),
quotes = (el.find(opts.quoteSelector).length) ? el.find(opts.quoteSelector) : el,
ocontainer = $(opts.container),
win = $(window),
root = $('html, body');
quotes.each(function(index) {
var quote = $(this), text = quote.text(), container, fontFace, backFace;
quote.addClass('fQ_quote');
container = ocontainer.eq(index);
container.addClass('fQ_container').css({'font-size': opts.fontSize});
frontFace = $('<div/>', {'class': 'fQ_front', html:'<q>'+ text +'</q>'});
backFace = $('<div/>', {'class': 'fQ_back'});
frontFace.add(backFace).appendTo(container)
.css({
'background-color': opts.bgColor,
'color': opts.fontColor,
'padding': opts.pads,
'width': container.width() - opts.pads * 2
}).find('q').css('quotes', opts.quoteMarks);
container.click(function() {
if(quote.offset().top + quote.height() - 50 < win.scrollTop()){
root.animate({
scrollTop: quote.offset().top - 20
}, 'slow', function(){
flash(quote);
});
}else if(quote.offset().top + 50 > win.scrollTop() + win.height()){
root.animate({
scrollTop: (quote.offset().top + quote.height() + 20) - win.height()
}, 'slow',function(){
flash(quote);
});
}else {
flash(quote);
}
});
container.height(frontFace.innerHeight());
backFace.innerHeight(frontFace.innerHeight());
});
win.scroll(function() {
ocontainer.each(function(index, element) {
if(win.scrollTop() + win.height() > $(this).offset().top + $(this).height()){
$(this).addClass('fQ_flip');
$(this).find('div').css({
'-moz-transition': opts.flipDuration,
'-webkit-transition': opts.flipDuration,
'transition': opts.flipDuration
});
if(!css3dsupport){
$(this).find('.fQ_front').show();
$(this).find('.fQ_back').hide();
}
}else{
$(this).removeClass('fQ_flip');
if(!css3dsupport){
$(this).find('.fQ_front').hide();
$(this).find('.fQ_back').show();
}
}
});
});
function flash(el){
el.css({
'background-color': opts.bgColor,
'color': opts.fontColor
});
setTimeout(function(){
el.css({
'background-color':'inherit',
'color':'inherit'
});
}, 1000);
}
function css3dsupport(){
var DOM = document.createElement('p'),
props = ['perspectiveProperty', 'WebkitPerspective', 'MozPerspective'];
for(var i=0; i<props.length; i++){
if(props[i] in DOM.style){
return true;
}
}
return false;
}
});
}
})(jQuery);