最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set different positions for multiple elements with the same class - Stack Overflow

programmeradmin4浏览0评论

I have a CSS class:

.something{
//all my css stuff here
}

I have 10 different elements that use this class:

<div class='something'></div>
<div class='something'></div>
<div class='something'></div>
<div class='something'></div>
<div class='something'></div> //ect ect....

Each of those divs need to be positioned differently and I don't want to make like 10 copy's of .something and point each one at a different class because its only the position that needs to change.

Also, I'm using JQuery to animate these divs, like:

$('.something').animate();

But that will animate all the divs attached to that class right? Is there any way around that?

I have a CSS class:

.something{
//all my css stuff here
}

I have 10 different elements that use this class:

<div class='something'></div>
<div class='something'></div>
<div class='something'></div>
<div class='something'></div>
<div class='something'></div> //ect ect....

Each of those divs need to be positioned differently and I don't want to make like 10 copy's of .something and point each one at a different class because its only the position that needs to change.

Also, I'm using JQuery to animate these divs, like:

$('.something').animate();

But that will animate all the divs attached to that class right? Is there any way around that?

Share Improve this question edited Dec 20, 2011 at 23:26 ThinkingStiff 65.4k30 gold badges147 silver badges241 bronze badges asked Dec 20, 2011 at 18:31 user849137user849137 2
  • 3 "Bare with me"...well ok, if it helps... <removes clothes> – Mike Robinson Commented Dec 20, 2011 at 18:40
  • haha you know what I meant... – user849137 Commented Dec 20, 2011 at 18:44
Add a ment  | 

4 Answers 4

Reset to default 6

You can specify an ID as well. So, you can do something like this:

<div class="something" id="s1"></div>
<div class="something" id="s2"></div>

Then:

$('#s1').animate({ ... }, speed); // animation of element 1
$('#s2').animate({ ... }, speed); // different animation of element 2

And still apply styles like:

.something {
    /* css */
}

Or, alternatively, you can not use IDs and simply loop: edited for simplicity

$('.something').animate({"top": "+=100px"}, speed);

If you'll be animating each div in somewhat the same way.

You can use each to reference every object one at a time.

$('.classname').each(function(){ });

Add ids to your <div>s:

<div class="something" id="one"></div>
<div class="something" id="two"></div>

Move your positioning CSS out of .something and into:

#one {
    left: 20px;
}

#two {
    left: 40px;
}

Animate using the selector:

$( '#one' ).animate();

I'm not sure if i got you right. But you can also increase the value with animate.

$('.something').animate({"top": "+=100px"},300);

This is gonna ad 100px to the top of every Dom element that has the class "something"

发布评论

评论列表(0)

  1. 暂无评论