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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
// ---------------------------------
// ---------- SimpleMarquee ----------
// ---------------------------------
//Copyright (C) 2016 Fabian Valle
//An easy to implement marquee plugin. I know its easy because even I can use it.
//Forked from: https://github.com/conradfeyt/Simple-Marquee
//Re-Written by: Fabian Valle (www.fabian-valle.com) (www.obliviocompany.com)
//
// ------------------------
// Structure //
//
// *********************************** - marque-container - *************************************
// * *
// * ******************************* ****************************************************** *
// * * * * * *
// * * - marquee-content-sibling - * * - marquee-content - * *
// * * * * * *
// * ******************************* ****************************************************** *
// * *
// **********************************************************************************************
//
//// Usage //
//
// Only need to call the createMarquee() function,
// if desired, pass through the following paramaters:
//
// $1 duration: controls the speed at which the marquee moves
//
// $2 padding: right margin between consecutive marquees.
//
// $3 marquee_class: the actual div or span that will be used to create the marquee -
// multiple marquee items may be created using this item's content.
// This item will be removed from the dom
//
// $4 container_class: the container div in which the marquee content will animate.
//
// $5 marquee-content-sibling : (optional argument) a sibling item to the marqueed item that
// affects the end point position and available space inside the
// container.
//
// $6 hover: Boolean to indicate whether pause on hover should is required.
;(function ($, window, document, undefined){
var pluginName = 'SimpleMarquee';
function Plugin (element, options) {
this.element = element;
this._name = pluginName;
this._defaults = $.fn.SimpleMarquee.defaults;
this.settings = $.extend( {}, this._defaults, options );
this.marqueeSpawned = [];
this.marqueeHovered = false;
this.documentHasFocus = false;
//
this.counter = 0;
this.timeLeft = 0;
this.currentPos = 0;
this.distanceLeft = 0;
this.totalDistance = 0;
this.contentWidth = 0;
this.endPoint = 0;
this.duration = 0;
this.hovered = false;
this.padding = 0;
this.init();
}
function marqueeObj(newElement){
this.el=newElement;
this.counter=0;
this.name="";
this.timeTop=0;
this.currentPos=0;
this.distanceTop=0;
this.totalDistance=0;
this.contentWidth=0;
this.endPoint=0;
this.duration=0;
this.hovered=false;
this.padding=0;
}
//methods for plugin
$.extend(Plugin.prototype, {
// Initialization logic
init: function () {
this.buildCache();
this.bindEvents();
var config = this.settings;
//init marquee
if($(config.marquee_class).width() == 0){
console.error('FATAL: marquee css or children css not correct. Width is either set to 0 or the element is collapsing. Make sure overflow is set on the marquee, and the children are postitioned relatively');
return;
}
if(typeof $(config.marquee_class) === 'undefined'){
console.error('FATAL: marquee class not valid');
return;
}
if(typeof $(config.container_class) === 'undefined'){
console.error('FATAL: marquee container class not valid');
return;
}
if(config.sibling_class != 0 && typeof $(config.sibling_class) === 'undefined'){
console.error('FATAL: sibling class container class not valid');
return;
}
//create the Marquee
this.createMarquee();
},
// Remove plugin instance completely
destroy: function() {
this.unbindEvents();
this.$element.removeData();
},
// Cache DOM nodes for performance
buildCache: function () {
this.$element = $(this.element);
},
// Bind events that trigger methods
bindEvents: function() {
var plugin = this;
$(window).on('focus',function(){
plugin.documentHasFocus = true;
for (var key in plugin.marqueeSpawned){
plugin.marqueeManager(plugin.marqueeSpawned[key]);
}
});
$(window).on('blur',function(){
plugin.documentHasFocus = false;
for (var key in plugin.marqueeSpawned){
plugin.marqueeSpawned[key].el.clearQueue().stop();
plugin.marqueeSpawned[key].hovered = true;
}
});
},
// Unbind events that trigger methods
unbindEvents: function() {
$(window).off('blur focus');
},
getPosition: function(elName){
this.currentPos = parseInt($(elName).css('left'));
return this.currentPos;
},
createMarquee: function(){
var plugin = this;
var config = plugin.settings;
var marqueeContent = $(config.marquee_class).html();
var containerWidth = $(config.container_class).width();
var contentWidth = $(config.marquee_class).width();
var widthToIgnore = 0;
if (config.sibling_class != 0){
widthToIgnore = $(config.sibling_class).width();
}
var spawnAmount = Math.ceil(containerWidth / contentWidth);
$(config.marquee_class).remove();
if(spawnAmount<=2){
spawnAmount = 3;
} else {
spawnAmount++;
}
var totalContentWidth = (contentWidth + config.padding)*spawnAmount;
var endPoint = -(totalContentWidth - containerWidth);
var totalDistance = containerWidth - endPoint;
for (var i = 0; i < spawnAmount; i++) {
var newElement = false;
if(config.hover == true){
newElement = $('<div class="marquee-' + (i+1) + '">' + marqueeContent + '</div>')
.mouseenter(function() {
if ((plugin.documentHasFocus == true) && (plugin.marqueeHovered == false)){
plugin.marqueeHovered = true;
for (var key in plugin.marqueeSpawned){
plugin.marqueeSpawned[key].el.clearQueue().stop();
plugin.marqueeSpawned[key].hovered = true;
}
}
})
.mouseleave(function() {
if ((plugin.documentHasFocus == true) && (plugin.marqueeHovered == true)){
for (var key in plugin.marqueeSpawned){
plugin.marqueeManager(plugin.marqueeSpawned[key]);
}
plugin.marqueeHovered = false;
}
});
} else {
newElement = $('<div class="marquee-' + (i+1) + '">' + marqueeContent + '</div>') ;
}
plugin.marqueeSpawned[i] = new marqueeObj(newElement);
$(config.container_class).append(newElement);
plugin.marqueeSpawned[i].currentPos = (widthToIgnore + (contentWidth*i))+(config.padding*i); //initial positioning
plugin.marqueeSpawned[i].name = '.marquee-'+(i+1);
plugin.marqueeSpawned[i].totalDistance = totalDistance;
plugin.marqueeSpawned[i].containerWidth = containerWidth;
plugin.marqueeSpawned[i].contentWidth = contentWidth;
plugin.marqueeSpawned[i].endPoint = endPoint;
plugin.marqueeSpawned[i].duration = config.duration;
plugin.marqueeSpawned[i].padding = config.padding;
plugin.marqueeSpawned[i].el.css('left', plugin.marqueeSpawned[i].currentPos+config.padding +'px'); //setting left according to postition
if (plugin.documentHasFocus == true){
plugin.marqueeManager(plugin.marqueeSpawned[i]);
}
}
//end for
if(document.hasFocus()){
plugin.documentHasFocus = true;
}else{
plugin.documentHasFocus = false;
}
},
marqueeManager: function(marqueed_el){
var plugin = this;
var elName = marqueed_el.name;
if (marqueed_el.hovered == false) {
if (marqueed_el.counter > 0) { //this is not the first loop
marqueed_el.timeLeft = marqueed_el.duration;
marqueed_el.el.css('left', marqueed_el.containerWidth +'px'); //setting margin
marqueed_el.currentPos = marqueed_el.containerWidth;
marqueed_el.distanceLeft = marqueed_el.totalDistance - (marqueed_el.containerWidth - plugin.getPosition(elName));
} else { // this is the first loop
marqueed_el.timeLeft = (((marqueed_el.totalDistance - (marqueed_el.containerWidth - plugin.getPosition(elName)))/ marqueed_el.totalDistance)) * marqueed_el.duration;
}
} else {
marqueed_el.hovered = false;
marqueed_el.currentPos = parseInt(marqueed_el.el.css('left'));
marqueed_el.distanceLeft = marqueed_el.totalDistance - (marqueed_el.containerWidth - plugin.getPosition(elName));
marqueed_el.timeLeft = (((marqueed_el.totalDistance - (marqueed_el.containerWidth - marqueed_el.currentPos))/ marqueed_el.totalDistance)) * marqueed_el.duration;
}
plugin.marqueeAnim(marqueed_el);
},
marqueeAnim: function(marqueeObject){
var plugin = this;
marqueeObject.counter++;
marqueeObject.el.clearQueue().animate(
{'left': marqueeObject.endPoint+'px'},
marqueeObject.timeLeft,
'linear',
function(){
plugin.marqueeManager(marqueeObject);
});
},
callback: function() {
// Cache onComplete option
var onComplete = this.settings.onComplete;
if ( typeof onComplete === 'function' ) {
onComplete.call(this.element);
}
}
});
//end methods for plugin
$.fn.SimpleMarquee = function (options) {
this.each(function() {
if ( !$.data( this, "plugin_" + pluginName ) ) {
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
}
});
return this;
};
$.fn.SimpleMarquee.defaults = {
property: 'value',
onComplete: null,
duration: 20000,
padding: 10,
marquee_class: '.marquee',
container_class: '.simple-marquee-container',
sibling_class: 0,
hover: true
};
})( jQuery, window, document ); |
Partager