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

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

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.

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..

Trouble centering and dynamically resizing image using jQuery

Usually I'm able to figure things out given enough time (hence my first post here), but I've been beating my head against the wall for a while now on this one.
I'm trying to:
Center image using jQuery (it's an <img> tag, not background image on a <div>),
Load the correct image size on (window).load, and
Load a new img file at certain breakpoints as the window is re-sized.
Here is my current attempt. The centering script works perfectly. Also, the correct image file is loaded. However, I cannot get the re-size portion to work when the window is made wider or narrower (the image file does not dynamically reload).
CODE:
<!-- Center Floating Div Elements -->
<script>
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
return this;
}
$(window).load(function(){
$('.laptop').center();
window.onresize = function(event) {
$('.laptop').center();
}
});
</script>
<!-- Load correct image size on window load -->
<script>
$(function(){
if($(window).width() >= 0 && $(window).width() <= 900){
$("img").attr("src","/images/laptop-900.png");
}
else if($(window).width() > 900 && $(window).width() <= 1400){
$("img").attr("src","/images/laptop-1500.png");
}
else{
$("img").attr("src","/images/laptop-2100.png");
}
})
</script>
<!-- Re-load new image size on window resize -->
<script>
$(window).onresize = function(){
if($(window).width() >= 0 && $(window).width() <= 900){
$("img").attr("src","/images/laptop-900.png");
}
else if($(window).width() > 900 && $(window).width() <= 1400){
$("img").attr("src","/images/laptop-1500.png");
}
else{
$("img").attr("src","/images/laptop-2100.png");
}
});
</script>
The relevant HTML is just a tag with a class of "laptop", wrapping around an image tag (which is where I want the image source to dynamically change).
lharby: here was my attempt at your suggestion, couldn't get it to work either:
<script>
var picresize = $(function(){
if($(window).width() >= 0 && $(window).width() <= 900){
$("img").attr("src","/images/laptop-900.png");
}
else if($(window).width() > 900 && $(window).width() <= 1400){
$("img").attr("src","/images/laptop-1500.png");
}
else{
$("img").attr("src","/images/laptop-2100.png");
}
});
$(window).load(function(){
$picresize();
window.onresize = function(event) {
$picresize();
}
});
</script>
So to elaborate on the comments. I made this fiddle:
https://jsfiddle.net/lharby/5p2xdnaf/
There is now a single function called chImage which checks window width and can then be triggered on various events.
I've also stored window width in a variable to make the code a bit cleaner and easier to read (you could also set your sizes in variables, and then change them later if needed, so only write them once).
var chImage = function(){
var winWidth = $(window).width();
if(winWidth >= 0 && winWidth <= 900){
$("img").attr("src","http://placehold.it/350x150");
}
else if(winWidth > 900 && winWidth <= 1400){
$("img").attr("src","http://placehold.it/500x300");
}
else{
$("img").attr("src","http://placehold.it/800x400");
}
};
Then call this function and bind it to whatever events we want.
$(window).on("load resize", function(){
chImage();
});
EDIT
I did move the centering function below to make sure it wasn't intefering with this function, but the centering can be achieved with css. I will try and update.
Chris Coyier has made an utterly interesting article about figuring out responsive images.
The image centering can be done in CSS, no need for scripts. See centering things and solved by flexbox.

Run script if screen size is less than 1024px

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();
}
}
});

Detecting a screen's width with Javascript

if(screen.availWidth > 850){
//Do this
} else {
//Do this
}
This is what I have right now. My issue right now is if someone was to zoom in to the page, I want the width to change as it will affect how the page is displayed.
Shouldn't you be more worried about someone resizing their browser window? Not everyone keeps their browsers maximized.
To do this:
if( (window.innerWidth || document.documentElement.clientWidth) > 850) {
// do something
}
else {
// do other thing
}
I'm fairly sure this takes the zoom into account, but I've never tested that.
Bind an event handler to the window.onresize event:
window.onresize = function(event) {
if (screen.availWidth > 850) { ... }
};
If you use jQuery:
$(window).resize(function(){
if ($(window).width() > 850) { ... }
});
Besides, if you want to create a responsive design, consider using CSS media queries. This automatically adapts the page if the user zooms or resizes the browser window and also works if the user has JavaScript deactivated.
/* CSS */
#media (min-width: 850px) {
/* style */
}

Categories