vendredi 3 avril 2015

combining statements with jquery, and using media queries for a back to top button

I am always confused with writing statements in jQuery. I'm trying to get this back-top button to only appear on larger screen widths and hide on mobile.


How do I combine statements or change up my code properly to get it to work?



$(document).ready(function(){
// Back to Top
$("#back-top").hide(); // hide #back-top first

// fade in after 500px down
$(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 500 && screen.width >= 641) {
$('#back-top').fadeIn();
} else {
$('#back-top').fadeOut();
}
});

// scroll body to 0px on click
$('#back-top').click(function () {
$('body,html').animate({
scrollTop: 0 }, 800); // 0px from top, 800 duration
return false;
});
});

if (screen.width < 641) {
$("#back-top").hide();
}
});

Aucun commentaire:

Enregistrer un commentaire