Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Advanced jquery question $(window).resize

Featured Replies

$(document).bind("scroll.myScroll", function () {
	
	var scrollTop = $(document).scrollTop();
	    masthead = $(".mast-head").outerHeight();
		notef = $(".note-f").outerHeight();
	    frontcontainheight = $(".center").outerHeight();
	    //derivative variable
	    	abpos = (frontcontainheight / 2.1);
	
	    sidebarwidth = $(".sidebarholder").width();
	    dataheight = $(".data-top").outerHeight(true);
		
		controlwidth = $(".loop-contain").outerWidth();
		innerwidth = $(".front-contain").outerWidth();
	    //get side-contain width dynamically 	
	    //derivative variable	
	    	sidecon = (controlwidth - innerwidth);

	    elem = $(".center");
		paddingTopBottom = elem.innerHeight() - elem.height();
	
		
		console.log (controlwidth);

		    if ($(window).width() > 1200) {
		        if (scrollTop > notef + dataheight + masthead - 50 + paddingTopBottom) {
		            $('.side-contain').css({
		            	position: "fixed",
			            left: sidebarwidth + innerwidth,
			            top: "50px",
			            width: sidecon - "24"
			            
		            });
		
		        }
		        if (scrollTop < notef + dataheight + masthead - 50 + paddingTopBottom) {
		            $('.side-contain').css({
		            position: "relative",
		            left: "",
		            top: "",
		            width: ""
		            });
		          
		        }
		        if (scrollTop > abpos + masthead - 50 + paddingTopBottom) {
		            $('.side-contain').css({
		            position: "absolute",
		            left: innerwidth,
		            top: abpos - notef, // if the scroll is more than half the height of front-contain then lock to that position, ie. the amount scroll of front-contain - 63px.
		
		            width: sidecon - "24"
		            
		            });
		     
		        }
		
		
		    } else if ($(window).width() < 1200) {
		          $('.side-contain').css({
		            position: "relative",
		            left: "",
		            top: "",
		            width: ""
		            });
		
		    }
		

});

 

This code locks a sidebar to the top of the screen by adjusting it's position from relative - fixed - absolute depending on the scroll of the page.

 

The problem I'm having is that when i resize the browser and the sidebar is in the position 'fixed' it obviously doesn't resize with the window.

 

Is there a way to get the function to fire WHEN the window is resized? I've tried $(window).resize around the if functions but it doesn't seem to work.

You can use

$(window).on('resize', function () { 
// Your code here
})
That will run your code as you resize the browser, is that what you are looking for?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.