November 11, 201313 yr $(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.
November 11, 201313 yr 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