I made a simple jquery script to sort content of the page on clicking of classes… in this example, all products, windows, or macintosh. The script works just as I want it to EXCEPT since I am using a # in the href the window scrolls… is there anyway to stop the window from scrolling and staying exactly where it is when the user clicks on one of the links?
Also, I put the script together pretty quick – if anyone wants to offer some optimization please go ahead…
basic html :
<a class="all" href="#">All Products</a>
<a class="win" href="#">Windows</a>
<a class="mac" href="#">Macintosh</a>
<div class="windows">1 win</div>
<div class="macintosh">2 mac</div>
<div class="windows">3 win</div>
<div class="windows">4 win</div>
<div class="windows">5 win</div>
<div class="macintosh">6 mac</div>
<div class="windows">7 win</div>
the script :
var $zproducthide = jQuery.noConflict();
$zproducthide(document).ready(function() {
$current = 'all';
$zproducthide(".all").click(function () {
if ($current != 'all'){
$zproducthide(".windows").hide();
$zproducthide(".macintosh").hide();
$zproducthide(".windows").fadeIn(1500);
$zproducthide(".macintosh").fadeIn(1500);
$current = 'all';
}
});
$zproducthide(".win").click(function () {
if ($current != 'windows'){
$zproducthide(".windows").hide();
$zproducthide(".macintosh").hide();
$zproducthide(".windows").fadeIn(1500);
$current = 'windows';
}
});
$zproducthide(".mac").click(function () {
if ($current != 'macintosh'){
$zproducthide(".windows").hide();
$zproducthide(".macintosh").hide();
$zproducthide(".macintosh").fadeIn(1500);
$current = 'macintosh';
}
});
});
How are you?
New answer (some optimization included):
Original answer:
The way to prevent this is to return false in the click event, this way, the default behaviour won’t execute! So:
I believe this should work!
Cheers!