jQuery doesn't execute when window size change - javascript

I just write a script to auto positioning an element on my page.
$(document).ready(function(){
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll < 180) {
$(".header-bottom").removeClass("scroll-menu");
if ($(window).width() > 991) {
$(".header_user_top").css("position", "absolute");
$(".header_user_top").css("top", "initial");
$(".header_user_top").css("right", "60px");
$(".header_user_top").css("z-index", "1");
}
} else {
$(".header-bottom").addClass("scroll-menu");
if ($(window).width() > 991) {
var rightContainer = ($(window).width() - ($('#header div.header-bottom div.container').offset().left + $('#header div.header-bottom div.container').outerWidth()));
$(".header_user_top").css("position", "fixed");
$(".header_user_top").css("top", "-22px");
$(".header_user_top").css("right", rightContainer+60+"px");
$(".header_user_top").css("z-index", "99");
}
}
});
});
It's working but if I resize browser window the script doesn't reload.
If I refresh the page it's working.
I have add window.onresize = function(){ location.reload(); }to my script to fix this problem but I'm looking for a better solution.
Any idea?

Use on function to call multiple event:
$(window).on("load resize scroll",function(e){
// do something
}

Call function which is handling size on windows.resize also
function handleSize(){
// You code to handle size
}
window.resize=handleSize();

Related

Unable to disable scroll function when resizing window

I have two menus that fade in and out on scroll. But when I resize the window I want to stop this event. I've tried plenty of stuff, searched a lot but nothing worked. I'm missing something. Here's what I have:
var scrollHandler = $(window).scroll(function() {
var top = $(window).scrollTop();
if (top > 0) {
$('.menu').fadeOut('fast', function() {
$('.second-menu').fadeIn('fast');
});
} else {
$('.second-menu').fadeOut('fast', function() {
$('.menu').fadeIn('fast');
});
}
})
scrollHandler;
if ($(window).width() < 768) {
$(window).off("scroll", scrollHandler);
}
Any help would be great, thanks!
The $(window).width() is only evaluated at runtime (i.e. the width of the viewport when the script is executed). It is not reactive in the sense that it will be updated on-the-fly when the viewport is resized.
Therefore, if you want to listen to changes in the width, you will have to place the logic within the window resize event callback:
$(window).resize(function() {
if ($(window).width() < 768) {
$(window).off('scroll');
}
});
Moreover, there are several issues with your code.
The scrollHandler should reference/define the function, not the outcome of the binding
Calling scrollHandler does not do anything. If you make the changes as per #1, then you can simply bind the logic using $(window).on('scroll', scrollHandler);
The .off() method does not accept a second parameter, this is enough: $(window).off('scroll')
After refactoring, your code will look something like this:
var scrollHandler = function() {
var top = $(window).scrollTop();
if (top > 0) {
$('.menu').fadeOut('fast', function() {
$('.second-menu').fadeIn('fast');
});
} else {
$('.second-menu').fadeOut('fast', function() {
$('.menu').fadeIn('fast');
});
}
};
// Bind scrollHandler firing to scroll event
$(window).on('scroll', scrollHandler);
$(window).resize(function() {
if ($(window).width() < 768) {
$(window).off('scroll');
}
});
Note:
If you want to get the best performance out of this, you should throttle the resize event so that the callback function is not fired too frequently. Lodash/Underscore.js have utility functions for that (_.throttle()), and there is also a jQuery plugin available.
In Lodash/Underscore.js:
$(window).resize(_.throttle(function() {
if ($(window).width() < 768) {
$(window).off('scroll');
}
}, 100));
Using Ben Alman's jQuery plugin:
$(window).resize($.throttle(100, function() {
if ($(window).width() < 768) {
$(window).off('scroll');
}
}));

have to refresh to run function

I'm trying to create a responsive thumbnail gallery for my portfolio website.
.gallerythumb is the thumb, and it's default width is 25%. My jquery, in theory, will resize the width of the thumbnail div containers based on the size of the page. It works, but the if/else statement only runs on page load, meaning I have to refresh the page, after I have resized the browser, to get it to run. Any clue what's going on?
if ($(window).width() < 640) {
$(".gallerythumb").css("width", "50%");
}
else if ($(window).width() < 260) {
$(".gallerythumb").css("width", "100%");
}
You can move your gallery resize code in a function and then call that function on window resize as well as window load
function resizeGallery() {
if ($(window).width() < 640) {
$(".gallerythumb").css("width", "50%");
}
else if ($(window).width() < 260) {
$(".gallerythumb").css("width", "100%");
}
}
$(window).resize(function () {
resizeGallery();
});
$(document).ready(function() {
resizeGallery();
})

How to run a jquery code by scrolling for once

I have a jquery code that I've indicated below. These codes run, When I start scrolling (I mean when the height of scroll is more than 100)
But I want to run just for once, immediately after height of 100, and not to be run more than once.
$(window).scroll(function(){
if ($(window).scrollTop() > 100 ){
//some codes...
}
});
How can I do this? Thank you :)
As soon as your condition is met, unbind the scroll handler:
$(window).scroll(function(){
if ($(window).scrollTop() > 100 ){
//Kill the handler
$(window).off("scroll");
//some codes...
}
});
use
$(window).unbind('scroll');
When you enter first time
$(window).scroll(function(){
if ($(window).scrollTop() > 100 ){
$(window).unbind('scroll');
//some codes...
}
});
Bind the scroll event to $(window), then when scrollTop() > 100, execute your action then unbind the scroll event from $(window)
$(document).ready(function() {
$(window).bind('scroll', function() {
if ($(this).scrollTop() > 100) {
$('body').prepend('Scroll top > 100');
$(window).unbind('scroll');
}
});
});
body { min-height: 1000px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
http://codepen.io/pen/
ok this can be done with the help of global variable or flag in order to see the effect first put lots of data into the page so that you can see vertical scroll.
<script>
var flag = 0;
$(document).ready(function() {
$(window).scroll(function() {
if (flag == 0) {
if ($(window).scrollTop() > 100) {
alert('1st time');
flag = 1;
}
} else if (flag == 1) {
alert('second time and u can comment this alert');
}
});
});
</script>
The .off() function will stop the function from firing after the first time.
$(document).ready(function () {
$(window).on('scroll.myEvent', function() {
if ($(document.body.scrollTop > 100))
{
$(window).off('scroll.myEvent');
alert('fired');
}
});
});
Example: http://jsfiddle.net/2k3s9jx8/

Responsive jquery click function

i want for specific width, specific click animations but it's not working. Why?
Here an example:
From 0 - 959px > anim1 From 960 - 1279px > anim2 From 1280 - 1699 > anim3 From 1700 - open end > anim4
Here is my Code:
$(document).ready(function(){
if ($(window).width() > 959) {
$("#mains").click(function(){
anim1();
});
}
});
$(document).ready(function(){
if ($(window).width() > 1279) {
$("#mains").click(function(){
anim2();
});
}
});
$(document).ready(function(){
if ($(window).width() > 1699) {
$("#mains").click(function(){
anim3();
});
}
});
$(document).ready(function(){
if ($(window).width() < 1700) {
$("#mains").click(function(){
anim4();
});
}
});
You are binding the animation on $(document).ready() based on the screen size. For example when the DOM is ready and the window width is less than 1700 only the function with anim4() will be used. What you need to do is place the window width check inside a single binding.
For example:
$(document).ready(function(){
$("#mains").click(function() {
if ($(window).width() < 959) {
// do something
} else if ($(window).width() < 1700) {
// do something else, etc.
}
});
});
It seems you have mistaken the conditionals. Also you can use just one document ready event handler and use an if else if statement to distinguish between cases. The statements should specifically set the width borders. Something like:
if ($(window).width() < 969 ) {
anim1();
} else if ($(window).width() >= 970 && $(window).width() < 1200) {
anim2();
}
The widths are as everyone can see examplary.
Except that there could be errors in anim* functions, but those errors would definitely mess up the code.
You are binding events on the basis of width size. I would recommend to check width in event handler and perform action accordingly
$(document).ready(function () {
$("#mains").click(function () {
if ($(window).width() > 959) {
anim1();
}
if ($(window).width() > 1279) {
anim2();
}
if ($(window).width() > 1699) {
anim3();
}
if ($(window).width() < 1700) {
anim4();
}
});
});
Read through your existing code and think about what it will do if the window width happens to be 1750:
Your first if test with > 959 will be true so it will bind a click handler that does anim1().
The second if test with > 1279 will also be true so it will bind another click handler that does anim2().
The > 1699 test would also be true, binding a third click handler that does anim3().
The fourth if test would be false since it has < rather than >, but in fact for a width of 1750 you want anim4() to run.
So then a click would call the first three animations.
What you need is an if/else if/else if/else structure so that only one of the four animX() functions is used. Also you probably want to do the test inside the click handler so that it uses the window width at the time of the click, not the width at the time the page loads. So:
$(document).ready(function() {
$("#mains").click(function() {
var w = $(window).width();
if (w < 960)
anim1();
else if (w < 1280)
anim2();
else if (w < 1700)
anim3();
else
anim4();
});
});
Note that at the beginning of the click handler I've put the $(window).width() into the (imaginatively named) w variable to save having to repeat those two function calls in the else if statements.
Try
$(document).ready(function(){
if ($(window).width() > 1699) {
anim3();
}else if($(window).width() > 1279){
anim2();
}else if($(window).width() > 959){
anim1();
}
});

jQuery: Add or Remove function in a specific window size

I am very new to jQuery/JS and I have little problem with add or remove function in a specific window size.
I would appreciate if you can take a look at my code and correct it.
In detail, I use tinyscrollbar plugin that I want only appear in a specific window size(let's say above 650px of window width).
Here is my code:
<!--****preload latest jQuery and tinyscrollbar plugin, then...****-->
<script type="text/javascript">
function newsScroll() {
$("#newsScroll").tinyscrollbar();
};
/*
if windows width is less than 650px, remove newsScroll function and
switch DIV ID to "spNewsScroll" and vice versa.
*/
function scrollOnOff(width) {
width = parseInt(width);
if (width < 650) {
$(window).unbind(newsScroll);
$("#newsScroll").attr('id','spNewsScroll');
} else {
$(window).bind(newsScroll);
$("#spNewsScroll").attr('id','newsScroll');
}
};
$(function() {
//get window size as of now.
scrollOnOff($(this).width());
$(window).resize(function() {
scrollOnOff($(this).width());
});
});
</script>
Try this, tested and working :-)
Live demo: http://jsfiddle.net/oscarj24/fUsnj/21/
function scrollOnOff(width) {
width = parseInt(width);
if(width < 650){
$(window).trigger('newsScroll');
$('#newsScroll').attr('id','spNewsScroll');
} else {
$('.scrollbar').hide();
$('#spNewsScroll').attr('id','newsScroll');
}
};
$(function() {
$(window).bind('newsScroll', function(e) {
$('.scrollbar').show();
$('#newsScroll').tinyscrollbar();
});
var screenWidth = $(this).width();
scrollOnOff(screenWidth);
$(window).on("resize", function(event){
var w = $(this).width();
scrollOnOff(w);
});
});
​

Categories