I built this object with functions that scrolls text, but when i try to add an instance to another element the previous instances get overwritten with the config options of the new instance.
Here is a example on jsfiddle after the top animation finishes the text speed and all other config options are overwritten.
HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Notes</title>
<script src="jquery-2.1.js"></script>
</head>
<body style="margin: 0 auto;">
<div class="scroll">
<div class="scrollContainer">
<div class="scrollText"></div>
</div>
</div>
<div>
<div>
<div id="me"></div>
</div>
</div>
<div>
<div>
<div id="try">I'll overwrite everything!</div>
</div>
</div>
<script src="scroll.js"></script>
</body>
</html>
JavaScript/jQuery Code:
$(document).ready(function () {
$(function () {
var scroll = [];
scroll = {
config: {
text: 'hello everybody',
speed: 15000,
container: $('.scrollText'),
width: 250,
parent: $('.scrollContainer'),
parentContainer: $('.scroll'),
textWidth: 0,
totalWidth: 0
},
init: function (config) {
$.extend(this.config, config);
var self = this,
selfC = self.config,
selfE = selfC.container;
console.log(selfE);
selfE.html(scroll.config.text.trim());
selfE.css({
display: 'inline-block'
})
selfC.textWidth = scroll.config.container.width();
//console.log(scroll.config.textWidth);
selfE.css({
width: selfC.textWidth + 10,
'margin-right': scroll.config.width,
'margin-left': scroll.config.width
})
selfC.totalWidth = selfE.outerWidth(true);
selfC.parentContainer.css({
width: scroll.config.width,
overflow: 'hidden',
margin: '0 auto'
})
scroll.animate(selfE);
},
animate: function (elem) {
var self = this,
selfC = self.config,
selfE = selfC.container;
selfE.animate({
'margin-left': '-=' + (selfC.totalWidth - selfC.width)
}, selfC.speed, function () {
selfE.css({
'margin-left': scroll.config.width
})
selfE.scrollText(selfC); //.speed -= 1000
});
}
};
$.fn.scrollText = function (config) {
return this.each(function () {
var obj = Object.create(scroll);
obj.init({
text: config.text,
speed: config.speed,
width: config.width,
container: $(this),
parent: config.parent || $(this).parent(),
parentContainer: config.parentContainer || $(this).parent().parent()
}, this)
});
}
$('#me, .scrollText').scrollText({
text: 'Help us update the names on our site by going to "Account" and selecting one of the options',
width: 250,
speed: 15000
});
$('div').last().scrollText({
text: 'another acroll',
speed: 5000,
width: 50
})
}())
})
I need away to apply scrollText to as many elements with custom configuration without overwriting.
N.B. when calling .scrollText with more then one element as a selector it does work as in the code above but the config options are the same for all elements/instances