Reputation: 79
(Thank you to anyone kind enough to help)
I'm having trouble using the jQuery hover method on a single instance of an element with a class name shared by multiple elements
Below I have provided my code:
<div class="container">
<div class="c_box">
<div class="img">
<svg hieght="0" width="0">
<defs>
<clipPath id="svgPath">
<circle stroke="#000000" cx="125" cy="235" r="125"></circle>
<rect x="0" y="0" width="250" height="230"></rect>
</clipPath>
</defs>
</svg>
<div class="rectangle">
<div class="semi_circle"></div>
</div>
<span class="text">Painting</span>
</div>
</div>
<div class="triangle"></div>
</div>
<div class="container">
<div class="c_box">
<div class="img">
<svg hieght="0" width="0">
<defs>
<clipPath id="svgPath">
<circle stroke="#000000" cx="125" cy="235" r="125"></circle>
<rect x="0" y="0" width="250" height="230"></rect>
</clipPath>
</defs>
</svg>
<div class="rectangle">
<div class="semi_circle"></div>
</div>
<span class="text">Painting</span>
</div>
</div>
<div class="triangle"></div>
</div>
$(document).ready(function (){
$(".container").hover(function(){
$(".c_box").animate({top: "-37px" }, 250);
$(".rectangle").delay(250).animate({right: "50px" }, 250);
$(".text").delay(450).animate({left: "69px" }, 150);
$(".triangle").delay(450).animate({top: "-20px" }, 150);
},
function(){
$(".text").animate({left: "-105px" }, 150);
$(".rectangle").delay(150).animate({right: "280px" }, 150);
$(".c_box").delay(250).animate({top: "-360px" }, 250);
$(".triangle").delay(450).animate({top: "0px" }, 150);
}
);
});$(document).ready(function (){
$(".container").hover(function(){
$(".c_box").animate({top: "-37px" }, 250);
$(".rectangle").delay(250).animate({right: "50px" }, 250);
$(".text").delay(450).animate({left: "69px" }, 150);
$(".triangle").delay(450).animate({top: "-20px" }, 150);
},
function(){
$(".text").animate({left: "-105px" }, 150);
$(".rectangle").delay(150).animate({right: "280px" }, 150);
$(".c_box").delay(250).animate({top: "-360px" }, 250);
$(".triangle").delay(450).animate({top: "0px" }, 150);
}
);
});
The code works fine, however, both the divs with the class 'container' are effected. Ideally i would like to only affect one div at a time (the one the mouse interacts with)
I'm familiar with the '$(this)' selector. Although, I have know idea if it's applicable in this case.
Upvotes: 0
Views: 56
Reputation: 4608
You should target the .c_box
, .rectangle
, .text
, .triangle
of the current .container
. The current container can be targeted using $(this)
.
$(function () {
$('.container').hover(function () {
var $currentContainer = $(this);
var $cBox = $currentContainer.find('.c_box');
var $rectangle = $currentContainer.find('.rectangle');
var $text = $currentContainer.find('.text');
var $triangle = $currentContainer.find('.triangle');
$cBox.animate({ top: '-37px' }, 250);
$rectangle.delay(250).animate({ right: '50px' }, 250);
$text.delay(450).animate({ left: '69px' }, 150);
$triangle.delay(450).animate({ top: '-20px' }, 150);
}, function () {
var $currentContainer = $(this);
var $cBox = $currentContainer.find('.c_box');
var $rectangle = $currentContainer.find('.rectangle');
var $text = $currentContainer.find('.text');
var $triangle = $currentContainer.find('.triangle');
$text.animate({ left: '-150px' }, 150);
$rectangle.delay(150).animate({ right: '280px' }, 150);
$cBox.delay(250).animate({ top: '-360px' }, 250);
$triangle.delay(450).animate({ top: '0' }, 150);
});
});
Upvotes: 1