I’m having a problem using the :not() selector/excluder. What I’m doing is adding an ‘.Activated’ class to opened accordion panels (with class’.panel’). Then when hovering all elements with the ‘.panel’ class I want to run a function. However there will sometimes be elements with both classes ‘class=”panel Activated”‘ that I don’t want to run the function on.
Also here is some of my relevant code:
function onOpenPanel(obj){
var slideR = obj.index + 1;
$('.panel:nth-child(' + slideR + ')').addClass('Activated');
}
$('.panel:not(.Activated)').hover(function(){
$(this).css('background-position','0px top');
},function(){
$(this).css('background-position','-41px top');
});
You’ll notice on the site that on the opened accordion panels the background jumps 41px because the my :not() selector isn’t picking up that the panel also has the ‘.Activated’ class and therefore shouldn’t be affected by the .hover function.
HTML:
<div class="accordion">
<!-- First slide -->
<div>
<img src="img/img-1.jpg" width="10" />
<div class="caption">
<p class="mask-1"></p>
<p class="title sl1">
Professional Products & Solutions
</p>
<p class="body one">As a global leader across many sectors, Sony Professional combine world class knowledge with groundbreaking technology – inspiring businesses to amaze their customers.<br />
</p>
</div>
</div>
<!-- First slide -->
<!-- Second slide -->
<div>
<img src="img/img-2-1.jpg" />
<div class="caption">
<p class="mask-2"></p>
<p class="title sl2">
4K Digital Cinema
</p>
<p class="body two">Taking digital cinema to new levels of immersive engagement, the superior resolution of Sony 4K creates the ultimate in crowd-pleasing experiences – partner with us for business growth.
</p>
<a href="http://stg.sony.co.uk/pro/hub/digital-cinema" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a>
</div>
</div>
<!-- Second slide -->
<!-- Third slide -->
<div class="sonySlideIn">
<img src="img/img-3.jpg" />
<div class="caption">
<p class="mask-3"></p>
<p class="title sl3">
Broadcast & Pro AV
</p>
<p class="body three">Shoot, edit, broadcast, archive. Whatever your priority – from image quality to production efficiency – Sony enables you realise your vision without compromises.
</p>
<img src="img/prod-3.jpg" width="400" height="98" id="prod-3"/>
<a href="http://stg.sony.co.uk/pro/hub/broadcast-professional-audio-video" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a>
</div>
</div>
<!-- Third slide -->
<!-- Fourth slide -->
<div class="sonySlideIn">
<img src="img/img-4.jpg" />
<div class="caption">
<p class="mask-4"></p>
<p class="title sl4">
Industrial Cameras
</p>
<p class="body four">From machine vision to visual communications and OEM conference solutions, see how the application of Sony imaging expertise delivers market-leading solutions for industrial environments.
</p>
<img src="img/prod-4.jpg" id="prod-4"/>
<a href="http://stg.sony.co.uk/pro/hub/industrial-machine-vision-cameras" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a>
</div>
</div>
<!-- Fourth slide -->
<!-- Fifth slide -->
<div class="sonySlideIn">
<img src="img/img-5.jpg" />
<div class="caption">
<p class="mask-5"></p>
<p class="title sl5">
Medical
</p>
<p class="body five">Driving the future of medical imaging by redefining clarity across monitors, cameras, radiology imagers, printers and recorders, Sony Medical creates pioneering solutions – including 3D – that enable clearer diagnoses and more efficient workflows.
</p>
<img src="img/prod-5.jpg" id="prod-5"/>
<a href="http://stg.sony.co.uk/pro/hub/medical" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a>
</div>
</div>
<!-- Fifth slide -->
<!-- Sixth slide -->
<div class="sonySlideIn">
<img src="img/img-6.jpg" />
<div class="caption">
<p class="mask-6"></p>
<p class="title sl6">
Projectors, Displays & Digital Signage
</p>
<p class="body six">Designed to attract attention, engage interest and add the wow factor to professional environments, Sony bring superior image quality to businesses, organisations, education establishments and venues of every size.
</p>
<img src="img/prod-6.jpg" id="prod-6"/>
<a href="http://stg.sony.co.uk/pro/hub/displays-projectors-digital-signage" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a>
</div>
</div>
<!-- Sixth slide -->
<!-- Seventh slide -->
<div class="sonySlideIn">
<img src="img/img-7.jpg" />
<div class="caption">
<p class="mask-7"></p>
<p class="title sl7">
Sports & Stadiums
</p>
<p class="body seven">Maximising customer engagement and satisfaction, our range of leading AV solutions for large arenas drives footfall, spend, loyalty and advocacy – see the advantages of partnering with Sony.
</p>
<a href="http://stg.sony.co.uk/pro/hub/solutions-stadiums-arenas" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a>
</div>
</div>
<!-- Seventh slide -->
<!-- Eigth slide -->
<div class="sonySlideIn">
<img src="img/img-8.jpg" />
<div class="caption">
<p class="mask-8"></p>
<p class="title sl8">
Video Security
</p>
<p class="body eight">See the most technically advanced, network based, end-to-end video security solutions – from hybrid and Full HD IP cameras to recorders and third-party compatible software.
</p>
<img src="img/prod-8.jpg" id="prod-8"/>
<a href="http://stg.sony.co.uk/pro/hub/video-security" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a>
</div>
</div>
<!-- Eigth slide -->
</div>
jQuery selectors return the elements that match when the code is executed, so they won’t reflect later changes to the DOM after that has happened.
Your code will bind
hoverevent handlers to the elements that didn’t have theActivatedclass when the code executed (presumably at DOM ready), but adding the class won’t remove those event handlers.If you want them to react dynamically, take a look into event delegation.