Run script if screen size is less than 1024px - javascript

I'm running a script on my page that make the div id="homesplash" disappear when user scrolls beyond 600px as follows:
$(window).scroll(function() {
if ($(this).scrollTop()>600)
{
$('#homesplash').hide();
}
else
{
$('#homesplash').show();
}
});
I need to figure out how to run this script only if the browser width is greater than 1024px. Any ideas?
I've tried to implement some code from a related post I found here, but I can't get it to work as I am unfamiliar with writing any javascript.
Thanks.

You can check $(window).width() and compare it to 1024. Something like:
$(window).scroll(function() {
if ( $(this).width() > 1024 ) {
$("#homesplash").toggle( $(this).scrollTop() <= 600 );
}
});

Working with your current code, you could use $(window).width() to get the width:
$(window).scroll(function() {
var windowWidth = $(window).width();
if (windowWidth > 1024) {
if ($(this).scrollTop() > 600) {
$('#homesplash').hide();
}
else {
$('#homesplash').show();
}
}
});

Related

fixed navbar issue after resize to page

I have a page if you click you are gonna see demo page and there is a fixed menu which is hidden.
after scroll page to down you'll see fixed menu set as display:block as you see on image:
but after appear if I'm resizing to window and if I turn normal desktop mode after scroll page to up as you see my fixed menu is not hiding
and another problem is if you open page mobile emulator (like on this emulator)[http://mobiletest.me/google_nexus_7_emulator/?u=http://firatabak.com/test/tur_detay.html] normally menu has to be show when I scroll page to down but it's not.
JS CODE
var navOffset = jQuery(".after-scroll-sticky").offset().top;
jQuery(window).scroll(function(){
var scrollPosition = jQuery(window).scrollTop();
if(scrollPosition >= navOffset){
jQuery(".sticky-navbar").fadeIn().addClass("fixed");
}else{
jQuery(".sticky-navbar").fadeOut().removeClass("fixed");
}
});
if ($(window).width() < 768) {
var navOffset2 = jQuery(".after-scroll-sticky").offset().top+200;
jQuery(window).scroll(function(){
var sP = jQuery(window).scrollTop();
if(sP >= navOffset2){
$(".sticky-navbar").addClass("fadeOutRightBig");
$(".menu-btn").fadeIn("fast");
}else{
$(".sticky-navbar").removeClass("fadeOutRightBig");
$(".menu-btn").fadeOut("slow");
}
});
}
Since you're defining the second jQuery.scroll function within an if statement, it only becomes active if the window width is less than 768px at the moment the script runs - it doesn't kick in when the window is resized. Instead you could try this format:
jQuery(window).scroll(function(){
if ($(window).width() < 768) {
// calculations and animation go here
}
});
Or better yet, combine the two jQuery.scroll functions together:
jQuery(window).scroll(function(){
var navOffset = jQuery(".after-scroll-sticky").offset().top,
scrollPosition = jQuery(window).scrollTop();
if ($(window).width() < 768) {
if (scrollPosition >= navOffset + 200) {
// ...
} else {
// ...
}
else if (scrollPosition >= navOffset) {
// ...
} else {
// ...
}
});
Then just make sure that you're undoing the changes made in other cases before applying the new changes.

Disable Javascript function when browser is non-desktop

Given a script that makes the sidebar sticky when the user scrolls to a certain point (on desktop), this currently creates an issue where the footer doesn't show at the bottom on mobile/tablet devices, but gives the footer an infinitely scrollable blank space. How can I target the script to only work on desktop browsers?
I've tried targeting the desktop with a variation of navigator.userAgent.match == "", but this is disabling the functionality.
When the user is coming from a mobile/tablet, the footer should just show up on the page at the bottom as if the javascript function wasn't loaded.
$(function() {
var $sidebar = $(".related-articles"),
$window = $(window),
offset = $sidebar.offset(),
topPadding = 15;
$window.scroll(function() {
if ($window.scrollTop() > offset.top) {
$sidebar.stop().animate({
marginTop: $window.scrollTop() - offset.top + topPadding
}, 1);
} else {
$sidebar.stop().animate({
marginTop: 0
}, 1);
}
});
});
UPDATE: adding $(window).width() > 767 && to the first if(); statement fixed the issue.
I think you're looking for something like this:
if ( $(window).width() > 739) {
//Code here for any desktop
}
else {
//Code here for mobile
}
You can find device width sizes here:
http://mydevice.io/devices/

Using jQuery in certain screen sizes

I need a mobile navigation to stick after the user has scrolled a certain amount. When a user has scrolled 205px on desktop resolution the navigation will stick no problem.
How do I change this to 64px after the screen size has gone below 767px? and how do I cancel the desktop jQuery from taking effect on a mobile?
Current desktop javascript:
$(window).scroll(function(){
if ($(this).scrollTop() > 205) {
$('.sidemenu').addClass('fixed');
} else {
$('.sidemenu').removeClass('fixed');
}
});
Current mobile javascript:
function checkPosition() {
if (window.matchMedia('(max-width: 767px)').matches) {
$(window).scroll(function(){
if ($(this).scrollTop() > 64) {
$('.sidemenu').addClass('fixed');
} else {
$('.sidemenu').removeClass('fixed');
}
})
}
};
Suggestions would be much appreciated.
Thank you.
You can add a class mobile to your body for example when the matchmedia matches.
$(document.body).toggleClass('mobile', window.matchMedia('(max-width: 767px)').matches);
Once you have that, the checkPosition simply has to get the proper scrollTop value.
function checkPosition() {
var scrollY = $(document.body).hasClass('mobile') ? 64 : 205;
$('.sidemenu').toggleClass('fixed', $(window).scrollTop() > scrollY);
};
Or simply add the matchMedia test instead of the hasClass test.
Additionally, I expect the height of the "fixed container" to be dynamic.
Maybe something like:
var scrollY = $('header').height(); // just an idea ofcourse to get 64 or 205.
You can check screens size in resize wvent like this
var width;
$(window).resize(function () {
width = $("html").width();
});
than in scroll event (or in other place) you can check:
if (width <= 767) {
// do some for small screen
}
else if (width > 767 && width < 1200) {
// do some for medium screen
}
//if..

$(window).width() and $('body').scrollLeft() not working in IE and Firefox

Sample: http://kat.isltest.net/col/
This sample site is working in chrome. The function is the body is centered even it contains a wide div of 1600px. The scrollbar is then by default centered in every window size.
Now, the problem is this is not working in IE and Firefox.
Here's the code:
if ($(window).width() <= 1600) {
$('body').scrollLeft(0);
}
if ($(window).width() <= 1400) {
$('body').scrollLeft(110);
}
if ($(window).width() <= 1360) {
$('body').scrollLeft(130);
}
if ($(window).width() <= 1280) {
$('body').scrollLeft(170);
}
if ($(window).width() <= 1152) {
$('body').scrollLeft(235);
}
if ($(window).width() <= 1024) {
$('body').scrollLeft(300);
}
if ($(window).width() <= 800){
$('body').scrollLeft(400);
}
DO you guys have idea why?
Thanks a lot! :)
You need to wrap you code with a listener that executes when the window is resized, otherwise the scrollbars are adjusted only once:
$(window).on('resize', function() {
/* your code */
});
I guess the reason that chrome adjusts it's vertical scrollbar to the center when the width of the browser window is smaller then 1600px is default behaviour and has nothing to do with your code.
Edit
Further, you need to use $(document).scrollLeft() instead of $('body').scrollLeft() if you want the viewport to scroll.
If you want to do scroll on click any button then you can do following using jquery.
$('#next').click(function(event) {
event.preventDefault();
$('.surr-cor').animate({
scrollLeft: "+=1350px"
}, "slow");
});
Mention event for firefox because it works on events. You can replace .surr-cor to your div element or body

Why is my resize event not firing?

My DIV #sequence is to be full height of the browser window for window sizes greater than 920px in height. When its greater than 920px in height I want to fire a plugin, for window sizes lower than 920px I want the #sequence div to be the height set in my CSS file as a fallback. This works when the page is loaded, however I cannot get the resize event to fire, its unresponsive and I see no errors in console.
This is my code mostly taken from this post Do something if screen width is less than 960 px:
var eventFired = 0;
if ($(window).height() < 920) {
} else {
fitSequence();
eventFired = 1;
}
$(window).on('resize', function() {
if (!eventFired == 1) {
if ($(window).height() < 920) {
} else {
$(window).resize(function(){ fitSequence(); });
}
}
});
Just as an FYI here's the fitSequence plugin I'm referencing:
function fitSequence(){
var height=$(window).height();
$('#sequence').css('height',height-100+"px");
};
If condition is not correct:
if (!eventFired == 1) {
It should be:
if (eventFired != 1) {
And in the function part this is ambiguous part of code:
$('#sequence').css('height',height-100+"px");
It should be:
$('#sequence').css('height',(height-100)+"px");
^^ add brackets

Categories