对多个元素使用JQuery脚本
Use a JQuery script on multiple elements
我已经编写了一个脚本,当用户将鼠标悬停在按钮上时,可以进行div滚动,但我想修改此代码,使其多个实例在一个页面上生效。
我是JQuery的新手,所以我不知道的方法
http://jsfiddle.net/9GcM3/7/
为了澄清,我希望"容器"div的多个副本都以相同的方式工作,使用相同的脚本,使用各自的按钮。
HTML:
<div class="container">
<div class="content">
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
</div>
<a href="#" id="left">left</a>
<a href="#" id="right">right</a>
JQuery:
$(document).ready(function () {
var products = $('.product').length;
var width = products * 100;
$('.content').css('width', width + 'px');
if ($('.content').width() > $('.container').width()) {
$("#left").hover(function () {
animateContent("left");
}, function () {
$('.content').stop();
});
$("#right").hover(function () {
animateContent("right");
}, function () {
$('.content').stop();
});
}
});
function animateContent(direction) {
var animationOffset = $('.container').width() - $('.content').width();
if (direction == 'left') {
animationOffset = 0;
}
$('.content').animate({
"marginLeft": animationOffset + "px"
}, "fast");
}
试试这个,可以在类似的情况下使用(比如模式(:
行动中:jsfiddle
$.extend({
worker: new function () {
var _self = this;
var _container = null;
_self.initialize = function (container) {
_container = container;
_attachBehavior(container);
};
var _attachBehavior = function (container) {
var products = $('.product', container).length;
var width = products * 100;
$('.content', container).css('width', width + 'px');
if ($('.content', container).width() > $('.container').width()) {
$("#left", container).hover(function () {
animateContent("left", container);
}, function () {
$('.content', container).stop();
});
$("#right", container).hover(function () {
animateContent("right", container);
}, function () {
$('.content', container).stop();
});
}
var animateContent = function (direction, container) {
var animationOffset = $(container).width() - $('.content', container).width();
if (direction == 'left') {
animationOffset = 0;
}
$('.content', container).animate({ "marginLeft": animationOffset + "px" }, "fast");
};
};
}
});
$(document).ready(function () {
$('.container').each(function () {
$.worker.initialize($(this));
});
});
您需要稍微确定一下范围,使其与多个项目一起工作:
$('.content').each(function(){
//this now refers to the specific content div
var products = $(this).find('.product').length;
var width = products * 100;
$(this).css('width', width + 'px');
//I think you get the rest from here, just adapt everything to find and $(this)
});
JS
var Scroll = function( data ){
var tis = this;
this.data = data;
this.init = function(){
this.left = $(this.data.left);
this.right = $(this.data.right);
this.container = $(this.data.container);
this.content = $(this.data.content);
this.left.hover(function(){tis.animateContent("left")}, function(){tis.content.stop()});
this.right.hover(function(){tis.animateContent("right")}, function(){tis.content.stop()});
}
this.animateContent = function(direction){
var containerWidth = this.container.width();
var contentWidth = this.content.width();
if ( contentWidth > containerWidth ){
var animationOffset = containerWidth - contentWidth;
if (direction == 'left') animationOffset = 0;
this.content.animate({marginLeft : animationOffset + "px"}, "fast")
}
}
}
var a = new Scroll( { left: '#left', right: '#right', container: '#container', content: '#content' } );
var b = new Scroll( { left: '#left2', right: '#right2', container: '#container2', content: '#content2' } );
$(function(){
a.init();
b.init();
});
CSS
.container {
position: relative;
height:100px;
width:400px;
background:red;
padding:0 10px;
overflow: hidden;
}
.content {
position: absolute;
background:#eee;
height:70px;
white-space:nowrap;
}
.product {
height:80px;
width:100px;
display: inline-block;
}
HTML
<div class="container" id="container">
<div class="content" id="content">
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
</div>
</div>
<a href="#" id="left">left</a>
<a href="#" id="right">right</a>
<div class="container" id="container2">
<div class="content" id="content2">
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
</div>
</div>
<a href="#" id="left2">left</a>
<a href="#" id="right2">right</a>
相关文章:
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 导入jQuery脚本获胜'我不处理html文件
- 用于发现哪个JS/jQuery脚本正在冻结页面的工具或技术
- JQuery脚本在IE中不起作用
- 自定义jQuery脚本无法在Wordpress上运行
- jQuery脚本在googlechrome中不起作用
- 重新启动jquery脚本后,角度停止工作
- 一个简单的粘性头jQuery脚本的问题
- 如何让C#代码在页面加载时运行的jquery脚本之后运行
- 为什么我用于检查所需输入字段的jQuery脚本如此缓慢
- MVC5更改jquery脚本中的图像src
- Jquery脚本未在运行时加载编辑:意外的令牌
- 用于wooccommerce的简单JQuery脚本不起作用
- JQuery脚本没有'Don’我不能工作两次
- 在jQuery脚本中放置15秒的延迟
- jquery脚本只能在一个方向上正常工作
- 注入的元素和jQuery脚本.如何让他们一起工作
- 如果视图中没有Jquery脚本引用,Unobtrusive Javascript Validation是不起作用的
- 让两个 jquery 脚本像它们应该的那样协同工作
- 如何在 Django Form 中使用 Jquery 脚本的值