lundi 5 janvier 2015

jQuery onload screen size and screen resize

I've got some jquery that triggers some events if the window loads at size smaller than 630px or if the screen is resized to less than 620px.


I currently have it as 2 separare if/else statements - one for onload and one for window resize - is there anyway I can merge these so I don't have duplicate the functions for each one?


Here's my code - I've had a go but can't get it to work



$(function () {
var $window = $(window);
$(window).on('resize', function () {
if ($window.width() < 620) {
$("#prd_details_smaller").append( $(".product_title"), $(".product_code"), $(".prd_reviews") );
$("#short_desc").hide();
}else{
$(".col_prd_rgt").prepend( $(".product_title"), $(".product_code"), $(".prd_reviews") );
$("#short_desc").show();
};
});
if ($(window).width() < 620) {
$("#prd_details_smaller").append( $(".product_title"), $(".product_code"), $(".prd_reviews") );
$("#short_desc").hide();
} else {
$(".col_prd_rgt").prepend( $(".product_title"), $(".product_code"), $(".prd_reviews") );
$("#short_desc").show();
}
});

Aucun commentaire:

Enregistrer un commentaire