I'm trying to make a javascript effect on a div like a garage door.
Basically I'd have an absolute div on the back and another div on the front which would shrink from bottom to top base on window school.
I've found a similar jsfiddle, but it's doing it on the width instead on the height and I'd like the div top to stay fixed and shrink from bottom to top.
JSFiddle Code
HTML
<div id="added">
<div id="container">
My center div...
</div>
</div>
CSS
#added {
background: #eee;
height: 2000px;
overflow:hidden;
}
#container {
width: 800px;
height: 2000px;
background-color: #567;
margin: 0 auto;
position:relative;
}
JS
$(window).resize(function() {
$('#container').css({
top: ($(window).height() - $('#container').outerHeight()) / 2
});
});
// To initially run the function:
$(window).resize();
var $scrollingDiv = $("#container");
$(window).scroll(function() {
var winScrollTop = $(window).scrollTop() + 0,
zeroSizeHeight = $(document).height() - $(window).height(),
newSize = 800 * (1 - (winScrollTop / zeroSizeHeight));
$scrollingDiv.css({
width: newSize,
"marginTop": winScrollTop + "px"
}, 500, 'easeInOutSine');
});
Any help will be appreciated.
Thank You
You can try that :
var $scrollingDiv = $("#container"),
defaultHeight = parseInt($scrollingDiv.css('height')); // whatever is in your css as
$(window).scroll(function() {
var winScrollTop = $(window).scrollTop() + 0,
zeroSizeHeight = $(document).height() - $(window).height(),
newSize = defaultHeight * (1 - (winScrollTop / zeroSizeHeight));
$scrollingDiv.css({
height: newSize,
"marginTop": winScrollTop + "px"
}, 500, 'easeInOutSine');
});
Set Both to auto
CSS
#added {
background: #eee;
height: auto;
width: auto;
}
It automatically add scroll bar no need to apply java script
Related
I'm trying to make my header disappear when scrolling down and only re-appear when scrolling up. I can't get it to work:
http://jsfiddle.net/mxj562qt/
Any ideas where I'm going wrong?
HTML:
<div id="header" class="custom-header">
This is your menu.
</div>
<main>
This is your body.
</main>
<footer>
This is your footer.
</footer>
Javascript:
// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $("#header").outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if(Math.abs(lastScrollTop - st) <= delta)
return;
// If they scrolled down and are past the navbar, add class .nav-up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight){
// Scroll Down
$("#header").addClass('nav-up');
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$("#header").removeClass('nav-up');
}
}
lastScrollTop = st;
}
CSS:
body {
padding-top: 40px;
}
#header {
background: #f5b335;
height: 50px;
position: fixed;
top: 0;
transition: top 0.2s ease-in-out;
width: 100%;
}
.nav-up {
top: -50px;
}
main {
height: 2000px;
}
footer { background: #ddd;}
* { color: transparent}
It would appear that the CSS class doesn't get added but I'm not sure why. Am I referencing the Div in the wrong way?
So, I can see that the issue stems from this bit of code ...
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$("#header").removeClass('nav-up');
}
In my tests, the doc height was always > than the st + window height.
I did this ...
// Scroll Up
console.log('doc height: ', $(document).height());
console.log('st+window height: ', st + $(window).height());
if(st + $(window).height() < $(document).height()) {
$("#header").removeClass('nav-up');
}
// results from scrolling up + down
// doc height: 2058
// st+window height: 313
// doc height: 2058
// st+window height: 280
// doc height: 2058
// st+window height 1614
// doc height: 2058
// st+window height: 1580
Changing the aforementioned JS to this seems to get you where you need to be.
$("#header").removeClass('nav-up');
Then your CSS needed some work ...
I noticed that your top element wasn't applying due to the CSS selector priority.
.nav-up {
top: -50px !important;
}
The result: scrolling down, the nav bar hides, scrolling up, the navbar shows.
I forked your code below;
http://jsfiddle.net/itsbjk/aw6qb2mr/16/
The problem here is with your CSS. You have specified position:fixed; in your code and that bit of CSS overrides all the JS you are writing. Fixed will force your header to be visible no matter what you are doing. Instead, you could try this in your CSS:
body {
padding-top: 40px;
}
#header {
background: #f5b335;
height: 50px;
position: absolute;
top: 0;
transition: top 0.2s ease-in-out;
width: 100%;
}
.nav-up {
top: -50px;
}
main {
height: 2000px;
}
footer { background: #ddd;}
* { color: transparent}
The absolute property should make it disappear on scrolling. And also, your referencing of the <div> tag isn't wrong!
Please check fiddle:
http://jsfiddle.net/howtoplease/f8sXN/4/
I want to make .float sticky by jQuery to .right when scroll.
HTML code
<div class="main">
<div class="float">
float
</div>
<div class="right">
Stick float to me
</div>
</div>
CSS code
.main{
margin-bottom:30px;
}
.float{
background: #333333;
color: #FFFFFF;
float: left;
height: 40px;
margin-right: 20px;
width: 40px;
}
.right{
background: none repeat scroll 0 0 #AAAAAA;
height: 245px;
overflow: hidden;
}
This should do:
$(window).scroll(function(){
var st = $(this).scrollTop();
$('.main').each(function(){
var $this = $(this),
offset = $this.offset(),
h = $this.height(),
$float = $this.find('.float'),
floatH = $float.height();
if(st >= offset.top && st < offset.top + h - floatH){
$float.css({'position':'fixed'});
}else{
$float.css({'position':'absolute'});
}
})
});
CSS:
.main{
margin-bottom:30px;
/* set main to realtive so float won't move out bounds */
position:relative;
}
.float{
background: #333333;
color: #FFFFFF;
height: 40px;
width: 40px;
/* set top to 0 and position to absolute*/
top:0;
position:absolute;
}
.right{
background: none repeat scroll 0 0 #AAAAAA;
height: 245px;
overflow: hidden;
/* the float width:40 plus its margin-right:20 that I removed*/
margin-left:60px;
}
I've updated the answer to solve the issue on '.right' — maintaining the same width and position.
Similar to #UDB solution, but on that method (changing 'margin-top') I noticed the '.float' sometimes shaking especially on long scroll and scrolling fast this happens:
On my new solution we're only changing the position property so no issue so far.
See this jsfiddle.
Thanks also to #Zeaklous and #UDB ideas.
altered Mark S's answer to keep the .float on left side like the page whose link is provided in comment
$(window).scroll(function () {
var st = $(this).scrollTop();
$('.main').each(function () {
var $this = $(this),
offset = $this.offset(),
h = $this.height();
if (st >= offset.top && st < offset.top + h - 40) {
$this.find('.float').css({
'margin-top': st - offset.top + 'px'
});
} else if (st >= offset.top + h + 30/*.main margin-bottom*/) {
$this.find('.float').css({
'margin-top': 'auto'
});
}
})
});
DEMO
how do I find out if the user scrolled up to the top or down to the bottom in a scrollable container?
Does jQuery offer any mechanisms?
css:
#container {
display: block;
width: 250px;
height: 25px;
background: green;
}
#scrolling {
width: 250px;
height: 300px;
backround: red;
overflow: auto;
}
http://jsfiddle.net/Hmaf2/2/
Thanks a lot!
Pat
$('#somediv').scrollTop()
will tell you the position from the top
-edit-
$('#somediv').scroll(function(){
if($('#somediv').scrollTop()==0){
console.log('top')
}
})
Yes you can access the current scroll top value of a scrollable container.
Here working is jsFiddle.
$('#scrolling').scroll(function() {
var scrollTop = $(this).scrollTop();
console.log(scrollTop);
});
You can test the scroll position by comparing the height, scrollable height and scroll offset of the div:
$(document).ready(function(){
$('#scrolling').scroll(function(){
var scrollTop = $(this).scrollTop();
var iheight = $(this).innerHeight();
var sheight = this.scrollHeight;
var text = '';
if (scrollTop <= 5) {
text = 'top';
}
else if (scrollTop+5 >= sheight-iheight) {
text = 'bottom';
}
else {
text = scrollTop+' middle ('+iheight+','+sheight+')';
}
$('#result').text(text);
});
});
fiddle
This example has reserved 5 pixels from the top and bottom of the div's margin.
I am currently working with a jquery plugin for setting my background image. The issue i am having is with the CSS/JQuery making the page in-proportionate. My content lies inside the #container div so I have set its position to absolute(originally was relative). How can I have the content of the page be aligned in the center and keep the properties of a 100% height? EXAMPLE
This is before the jquery plugin- CSS 100% height- EXAMPLE
Jquery Background plugin
<script>
(function($) {
$.fn.fullBg = function(){
var bgImg = $(this);
function resizeImg() {
var imgwidth = bgImg.width();
var imgheight = bgImg.height();
var winwidth = $(window).width();
var winheight = $(window).height();
var widthratio = winwidth / imgwidth;
var heightratio = winheight / imgheight;
var widthdiff = heightratio * imgwidth;
var heightdiff = widthratio * imgheight;
if(heightdiff>winheight) {
bgImg.css({
width: winwidth+'px',
height: heightdiff+'px'
});
} else {
bgImg.css({
width: widthdiff+'px',
height: winheight+'px'
});
}
}
resizeImg();
$(window).resize(function() {
resizeImg();
});
};
})(jQuery)
</script>
CSS related to the issue
#container {
position: relative;
margin: 0 auto;
width: 945px;
background: #f0f0f0;
height: auto!important;
height: 100%;
min-height: 100%;
border-right: 15px solid #000;
border-left: 15px solid #000;
}
.fullBg {
position: absolute;
top: 0;
left: 0;
overflow: hidden;
}
HTML/Inline JS to call function
<img src="images/raven_bg.jpg" alt="" id="background" />
<div id="container">
</div>
<script>
$(window).load(function() {
$("#background").fullBg();
});///this comes right before the closing body tag
</script>
This centers it -
#container {
margin-left: 50%;
left: -488px;
}
it's kind of hacky, but it works.
50% shift (to keep it centered based on width)
half of width, plus border = 478.5 (or 488) to keep it centered in frame. And of course, with "left" only works because it's got position: relative; attached to it
I have a #sidebar (which starts below my #header div) and a #footer (around 120px off the bottom of the page).
I'm trying to make the sidebar scroll with the content of the page. The code below does this semi-successfully:
/* profile sidebar */
#sidebar>div{ width: 300px; margin-top: 10px; }
#sidebar.fixed>div{position:fixed;top:0;}
#sidebar.fixed_bottom>div{position:fixed;bottom:172px;}
jQuery(function ($) {
$.fn.scrollBottom = function() {
return $(document).height() - this.scrollTop() - this.height();
};
var el = $('#sidebar'),
pos = el.position().top;
$(window).scroll(function() {
if ($(this).scrollTop() >= pos) {
if ( $(this).scrollBottom() <= 172 ) {
el.removeClass('fixed');
el.addClass('fixed_bottom');
} else {
el.removeClass('fixed_bottom');
el.addClass('fixed');
}
} else {
el.removeClass('fixed');
}
});
});
The problem is, on smaller resolutions, this makes the sidebar "jump" once you reach a certain position on the page. It stops it from overlapping the footer (which is the problem if you remove the fixed_bottom class) but doesn't look good.
What I'd like to do is this: user scrolls to the bottom of the page, the sidebar scrolls along with the content until it reaches say 20px above the top of my footer, at which point it stays there until the user scrolls back up.
Thanks in advance,
I believe this should do what you want.
http://jsfiddle.net/FDv2J/3/
#sidebar>div{ width: 100px; margin-top: 10px; position:fixed; left: 0; top: 0;}
$(function() {
$.fn.scrollBottom = function() {
return $(document).height() - this.scrollTop() - this.height();
};
var $el = $('#sidebar>div');
var $window = $(window);
$window.bind("scroll resize", function() {
var gap = $window.height() - $el.height() - 10;
var visibleFoot = 172 - $window.scrollBottom();
var scrollTop = $window.scrollTop()
if(scrollTop < 172 + 10){
$el.css({
top: (172 - scrollTop) + "px",
bottom: "auto"
});
}else if (visibleFoot > gap) {
$el.css({
top: "auto",
bottom: visibleFoot + "px"
});
} else {
$el.css({
top: 0,
bottom: "auto"
});
}
});
});
I tried to break things up and name variables in such a way that it would be understandable. Let me know if there's anything you're unsure of. Notice that I added resize as well as scroll since it matters if the window changes size.
EDIT: Modified version using similar technique to the original to find the upper bound:
http://jsfiddle.net/FDv2J/4/
$(function() {
$.fn.scrollBottom = function() {
return $(document).height() - this.scrollTop() - this.height();
};
var $el = $('#sidebar>div');
var $window = $(window);
var top = $el.parent().position().top;
$window.bind("scroll resize", function() {
var gap = $window.height() - $el.height() - 10;
var visibleFoot = 172 - $window.scrollBottom();
var scrollTop = $window.scrollTop()
if (scrollTop < top + 10) {
$el.css({
top: (top - scrollTop) + "px",
bottom: "auto"
});
} else if (visibleFoot > gap) {
$el.css({
top: "auto",
bottom: visibleFoot + "px"
});
} else {
$el.css({
top: 0,
bottom: "auto"
});
}
}).scroll();
});
body{
margin: 0;
}
#sidebar>div {
width: 100px;
height: 300px;
margin-top: 10px;
background-color: blue;
position: fixed;
}
#stuff {
height: 1000px;
width: 300px;
background-image: url("http://placekitten.com/100/100")
}
#footer,
#header {
height: 172px;
width: 300px;
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="header"></div>
<div id="sidebar">
<div class="fixed">sidebar</div>
</div>
<div id="stuff">
</div>
<div id="footer">
</div>