简体   繁体   English

使功能分别作用于每个元素

[英]Make function work on each element separately

I'm creating a wordpress element that will show post excerpts in a carousel. 我正在创建一个wordpress元素,该元素将在轮播中显示帖子摘录。 To demonstrate what I am doing I've created a github repository with a html version of the carousel. 为了演示我在做什么,我创建了一个带有html版本的轮播的github存储库。

https://github.com/dingo-d/Post-Excerpt-Carousel https://github.com/dingo-d/Post-Excerpt-Carousel

The carousel is working mostly fine, but I have an issue that I don't understand where it's coming from. 轮播工作正常,但我有一个问题,我不知道它来自哪里。 When I have one carousel on a page, all is working fine. 当我在页面上放一个旋转木马时,一切正常。 But what if there are two or more on the same page? 但是,如果同一页上有两个或多个?

What happens is that when you click on next icon, the slider where you've clicked it will move to the left, and the classes will switch from one item to another list item (active class), but those classes will move on all carousels on the site (there is a html file with multiple carousels on repo). 发生的是,当您单击下一个图标时,您单击的滑块将向左移动,并且类将从一个项目切换到另一个列表项(活动类),但是这些类将在所有轮播上移动在网站上(在回购中有一个带有多个轮播的html文件)。

I've created a function that will manage the movement and clicking, and I'm calling it like this: 我创建了一个函数来管理移动和点击,我这样称呼它:

$('.post_excerpt_carousel').each(function(){
    var $this = $(this);
    post_excerpt_positioning($this);
});

I've done it like this, so that I can toggle it on resize also. 我已经这样做了,所以我也可以在调整大小时切换它。

I've created a fiddle here but it's easier to see the repository and download the examples folder (better visibility). 我在这里创建了一个小提琴但是查看存储库和下载示例文件夹更加容易(可见性更好)。

How to prevent the classes switching on all carousels, and make them switch only when I click on a certain carousel? 如何防止所有轮播上的类切换,并且仅当我单击某个轮播时才使它们切换?

EDIT: Codepen preview: http://codepen.io/dingo_d/pen/doNyMw 编辑:Codepen预览: http ://codepen.io/dingo_d/pen/doNyMw

The problem was on line 61 in main.js : 问题出在main.js的第61行上:

var $a = $('.active');

You are selecting all the elements having .active class. 您正在选择所有具有.active类的元素。

You can fix this just by replacing this line with : 您可以通过以下方式解决此问题:

var $a = $('.active', $carousel);

This way, it will select only the .active found inside $carousel 这样,它将仅选择$carousel发现的.active

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM