简单的jQuery变量问题-变量冲突
Simple jQuery variable issue - conflicting variables
我想在这里使用这个伟大的jQuery文本滑块:http://codepen.io/mikedevelops/pen/eNjeJm
但是我真的需要在同一个页面上有两个滑块。我试图复制代码并更改第二个滑块中的变量名称,以防止出现问题-但似乎无法让它工作。下面是我的尝试,使用jquery: http://codepen.io/hardanger/pen/NqmOom
为了明确,我认为这是变量的问题。
// add as many adjectives as you like!
var adjs = ["very cool", "really exciting", " super awesome", "jQuery sliding"],
sentence = $('#sentence'),
adjList = $('#adjList'),
stop = false;
// function to return the most up-to-date
// version of our adjective list
function getList() {
return $('.adj');
}
// function to build the adjective list
// args: adjective array
function build(arr) {
for (i=0; i<arr.length; i++) {
var item = "<li class='adj'>";
$(adjList).append(item + arr[i] + "</li>");
}
}
// function to resize adjective list
// args: adjective list item
function resize(el) {
$(adjList).animate({
width: $(el).width(),
}, 200);
}
// function to add slide-in transition
// args: element that needs to slide
function slideIn(el) {
// duration slide is on screen
var hold = 1000;
// resize area to sliding element
resize($(el));
// add slide-in class
$(el).addClass('slide-in');
// after 'hold' duration slide-out current item
// then slide in next item
setTimeout(function(){
// check to see if loop should continue
if (stop === true) {
stop = false;
return;
}
// slide current element out
slideOut(el);
// slide in next element in queue
slideIn($(el).next());
}, hold);
}
// function to add slide-out transition
// args: element that needs to slide
function slideOut(el) {
// remove current class and add slide-out transition
$(el).removeClass('slide-in').addClass('slide-out');
// wait for slide tramsition to finish then
// call 'change' function
setTimeout(function(){
change();
}, 200);
}
// function to re-arrange adjective list
function change() {
// store last item index
var i = adjs.length - 1;
// detach element that has slide-out class
// put to the bottom of the list after
// the last item
$('.adj:eq(' + i + ')').after($('.slide-out').detach());
// remove class to send element back to original position
$('.adj').removeClass('slide-out');
}
// build slider
build(adjs);
// init loop
slideIn(getList()[0]);
// second slider
// add as many adjectives as you like!
var adjs2 = ["test", "test", " testtesttesttesttesttest", "test"],
sentence2 = $('#sentence2'),
adjList2 = $('#adjList2'),
stop = false;
// function to return the most up-to-date
// version of our adjective list
function getList2() {
return $('.adj2');
}
// function to build the adjective list
// args: adjective array
function build(arr2) {
for (i2=0; i2<arr2.length; i2++) {
var item2 = "<li class='adj2'>";
$(adjList2).append(item2 + arr2[i2] + "</li>");
}
}
// function to resize adjective list
// args: adjective list item
function resize(el2) {
$(adjList2).animate({
width: $(el2).width(),
}, 200);
}
// function to add slide-in transition
// args: element that needs to slide
function slideIn2(el2) {
// duration slide is on screen
var hold2 = 1000;
// resize area to sliding element
resize($(el2));
// add slide-in class
$(el2).addClass('slide-in2');
// after 'hold' duration slide-out current item
// then slide in next item
setTimeout(function(){
// check to see if loop should continue
if (stop === true) {
stop = false;
return;
}
// slide current element out
slideOut2(el2);
// slide in next element in queue
slideIn2($(el2).next());
}, hold2);
}
// function to add slide-out transition
// args: element that needs to slide
function slideOut2(el2) {
// remove current class and add slide-out transition
$(el2).removeClass('slide-in2').addClass('slide-out2');
// wait for slide tramsition to finish then
// call 'change' function
setTimeout(function(){
change();
}, 200);
}
// function to re-arrange adjective list
function change() {
// store last item index
var i2 = adjs2.length - 1;
// detach element that has slide-out class
// put to the bottom of the list after
// the last item
$('.adj2:eq(' + i2 + ')').after($('.slide-out2').detach());
// remove class to send element back to original position
$('.adj2').removeClass('slide-out2');
}
// build slider
build(adjs2);
// init loop
slideIn2(getList2()[0]);
封装代码的方法有很多种。在这种情况下,一个好的模式是作为jQuery插件。正确封装的关键是删除全局引用,以便隔离代码并注入代码所需的任何操作。
全局引用可能很棘手,因为它们可以以多种形式出现。在这种情况下,您在问题中链接的原始代码包含对JavaScript中的全局变量(例如,adjs
数组)和特定DOM元素(例如,$('#adjList')
和$('.slide-out')
)的引用。您必须小心使用类,因为当您按类选择时,您将获得页面上具有该类的所有元素。对于原始代码来说,这是一个问题,因为您只需要具有该类的元素的子集。
样式表在全局引用方面也有问题。在原始代码中,样式表使用id对元素进行样式设置,这意味着页面上只能有一个元素。你真的想使用可以应用于多个元素的类。
JavaScript代码真正需要知道的最基本的事情是文本将被交换的DOM元素(<ul id="adjList">
),以及要交换的文本列表。对jQuery插件的调用看起来像这样:
$('#adjList').textSlider(adjs);
我做了一个有点黑,但功能的jQuery插件从原始代码在这里:
http://codepen.io/anon/pen/JdVVje主要的变化是将适当的代码包装为插件,修复样式表中的ID引用,并修复JavaScript代码中的样式引用。如您所见,现在页面上有两个工作的文本滑块。
- React:未捕获的不变量冲突:ReactDOM.render():无效的组件元素
- Javascript 全局变量命名冲突
- JavaScript变量名称和HTML输入名称属性:名称空间冲突
- 多个脚本标记变量冲突
- 用冲突的局部变量重新定义全局变量
- 模态上的React-不变量冲突
- 客户端React-Router演示:不变量冲突
- 自定义React Native UI组件:不变量冲突
- 不变量冲突:对象作为React的子对象是无效的
- 使用ReactDOM.render时的不变量冲突错误
- Javascript变量冲突
- React不变量冲突:对象作为React的子对象是无效的
- ReactJS错误:不变量冲突
- 未捕获的不变量冲突:期望onClick侦听器是一个函数,而不是类型对象
- 简单的jQuery变量问题-变量冲突
- 调用React类的构造函数时的不变量冲突
- 使用Mocha进行React测试时的不变量冲突
- 使用JavaScript的变量冲突
- 不变量冲突:使用Link &表
- 应该避免使用与全局变量冲突的变量