Show a fixed DIV after scrolling some pixels - html

I use this code to show a fixed div after 800px of scrolling down
(function($){
$("#book-container").css({"visibility": "hidden"});
$(document).scroll(function () {
var y = $(this).scrollTop();
if (y > 800) {
$("#book-container").css({"visibility": "visible"});
$('#book-container').fadeIn(1000);
} else {
$('#book-container').fadeOut(100);
}
});
})( jQuery );
(function($) {
$(document).ready(function() {
$('.hide-me').on('click', function(){
$('.book-hotels').slideToggle(500);
$('.hide-me').hide(0).delay(510);
$('.hide-me').slideToggle(200);
});
});
})( jQuery );
#media (min-width:1710px) {#book-container {
position:fixed;
bottom:calc(50% - 200px);
right:0;
}
}
#media (min-width:1710px) {.hide-me {
float:right;
background:#cc0000;
padding:2px 6px 2px 6px;
cursor: pointer;
}
}
#media (min-width:1710px) {.book-hotels{
width:250px;
height:410px;
background:#fff;
padding:22px 0 0 0;
}
}
.hide-me span {
color:#fff;
font-size:12px;
font-family:"Roboto Slab";
font-weight:700;
}
#media (max-width:1710px) {.hide-me {
visibility:hidden;
}
}
#media (min-width:1710px) {.book-hotels h3 {
white-space: nowrap;
}
}
<div id="book-container">
<div class="hide-me"><span>Hide/Show</span></div>
<div class="book-hotels"><h3>Book a Hotel</h3>some content here..
</div>
</div>
It works but I have 2 problems.
When the page loads for the first time the div is visible even close to the top, I start to scroll and it disappears as it should and reappears again at 800px from the top, so how to make it not appear on load?
And, second, the script is in two pieces, I tried to merge into one but it doesn't work, I need a little help in that.
And finally, how can I add a function to disappear again the div before it reaches the footer?
Thank you

Fixed, by adding and remove a css rule to fixed div, visibility:hidden on load and visible after 800 pixels.

Related

Display a Search bar on header on scroll HTML/CSS

I have a search bar which would like to display onto the header on scroll, a great example is like the one on this site: https://www.indiamart.com/
Approach 1 - A simple way to do this would be to detect a scroll & add and remove a class that contains display: none;
You can have an event listener -
window.addEventListener('scroll', function() {
if( window.scrollY !== 0) {
document.getElementById('searchBar').classList.add('scrolled');
} else {
document.getElementById('searchBar').classList.remove('scrolled');
}
});
With the CSS -
.noScroll
{
background: yellow;
position:fixed;
height: 50px; /*Whatever you want*/
width: 100%; /*Whatever you want*/
top:0;
left:0;
display:none;
}
/*Use this class when you want your content to be shown after some scroll*/
.scrolled
{
display: block !important;
}
.parent {
/* something to ensure that the parent container is scrollable */
height: 200vh;
}
And the html would be -
<div class="parent">
<div class ='noScroll' id='searchBar'>Content you want to show on scroll</div>
</div>
Here's a JSFiddle of the same - https://jsfiddle.net/kecnrh3g/
Approach 2 -
Another simple approach would be
<script>
let prevScrollpos = window.pageYOffset;
window.onscroll = function() {
let currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById('searchBar').style.top = '-50px';
} else {
document.getElementById('searchBar').style.top = '0';
}
prevScrollpos = currentScrollPos;
}
</script>
with the html -
<div class="parent">
<div id ='searchBar'>Content you want to show on scroll</div>
</div>
and css
#searchBar {
background: yellow;
position: fixed;
top: 0;
left: 0;
height: 50px;
width: 100%;
display: block;
transition: top 0.3s;
}
.parent {
height: 200vh;
}
Here's a JSFiddle of the same - https://jsfiddle.net/0tkedcns/1/
From the same example, the idea is only to show/hide once user scroll the page using inline css display property, you can do the same or at least provide a code sample so we can help you!
HTML
<div class="search-bar">
<div class="sticky-search">
Sticky Search: <input type="text" value="search" />
</div>
</div>
CSS
.sticky-search {
display:none;
position:fixed;
top:0px;
left:0px;
right:0px;
background:blue;
padding:10px;
}
JS
var searchHeight = $(".search-bar").outerHeight();
var offset = $(".search-bar").offset().top;
var totalHeight = searchHeight + offset;
console.log(totalHeight);
$(window).scroll(function(){
if($(document).scrollTop() >= totalHeight) {
$('.sticky-search').show();
} else {
$('.sticky-search').hide();
}
});

Remove class at top of page isn't always working

I've got a header that I want to disappear (move above the top of the browser screen) when the user scrolls down, then when the user scrolls back up I want the header to reappear (move back down) and also to gain a solid white background behind it.
All of this is working fine, but then I want the white background to be removed if the user scrolls back up to the very top of the page again (so it's just the header visible but with no white background).
I've cobbled together the code for this, and it's pretty much working - but, for some reason, in certain circumstances, the white background isn't being removed?!
It seems to be if you've scrolled back up enough to make the header reappear with the white background, but not all the way top the top, and then scroll the rest of the way then the white background doesn't disappear for some reason.
Sorry, I've probably not explained that very well, but here's my code. If someone is able to replicate the bug I'm getting and offer an explanation I'd be very grateful!
Thanks
// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 30;
var navbarHeight = $('header').outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 30);
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').removeClass('header-down').addClass('header-up').removeClass('header-solid');
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('header').removeClass('header-up').addClass('header-down').addClass('header-solid');
}
}
lastScrollTop = st;
};
$(function () {
$(window).scroll(function () {
var top_offset = $(window).scrollTop();
if (top_offset == 0) {
$('header').removeClass('header-solid');
}
})
});
body {
margin: 0;
background: url('https://images.unsplash.com/3/doctype-hi-res.jpg?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=2246&q=80');
background-size: cover;
}
.wrapper {
width: 100%;
height: 3000px;
}
header {
display: flex;
width: 100%;
padding: 20px 50px;
position: fixed;
box-sizing: border-box;
transition: 300ms all ease-in-out;
margin-top: 0px;
}
.header-logo {
width: 200px;
}
.header-nav {
flex-grow: 1;
text-align: center;
}
.header-socials {
width: 200px;
text-align: right;
}
.header-up {
margin-top: -60px;
}
.header-down {
margin-top: 0px;
}
.header-solid {
background-color: #FFFFFF;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
<header class="header-down">
<div class="header-logo">
<img src="#" class="img-header-logo"/>
</div>
<div class="header-nav">
One Two Three Four Five
</div>
<div class="header-socials">
Social Links
</div>
</header>
</div>

How to show a div when another div is scrolled out of DOM

I have a fixed div(logo-fixed) that appears when scrolling 1070px from Top, but I don't want that, I like the div to appear when I scroll 50 pixels after another div(headerclass)
How can this be achieved?
9.
this is the code
(function($) {
$(".logo-fixed").css({"visibility": "hidden"});
$(document).scroll(function () {
var y = $(this).scrollTop();
if (y > 1070) {
$(".logo-fixed").css({"visibility": "visible"});
$('.logo-fixed').fadeIn(200);
} else {
$('.logo-fixed').hide(0);
}
});
})( jQuery );
.headerclass {
position:sticky;
top:0;
z-index:600;
}
.logo-fixed {
position:fixed;
top:0;
left:0;
width:224px;
height:120px;
z-index:1000;
background: url("#");
background-position: center center;
background-repeat: no-repeat;
background-size: contain;
}
<div class="headerclass"></div>
<div class="logo-fixed"></div>
You can get the position of the headerclass element with $(.headerclass').scrollTop() and then compare that to y in your conditional, instead of 1070.
Example:
if (y > 1070) {
gets replaced with
if (y > $(.headerclass').scrollTop() + 50 )

How to align Navbar at bottom of window, but have content under this in CSS?

I need help with this website I'm making for a project at school. I need help with the Nav bar at the bottom of the window when you first load the page but if you scroll down it scrolls with the page. I need it to basically align to the bottom of the window when its first loaded. I can't for the life of me figure out how to do it. Here is a reference.
Example Website
Notice how the Nav bar is always at the bottom of the window when scrolled to the top even if you resize the window. I would like to do this with only CSS and HTML, but I understand a little bit of Javascript so if It can't be done with only CSS and HTML it's alright. Thanks for the help.
The magic is in using both JQuery and CSS.
JQuery
$(document).ready(function () {
var navBarY = $(".bottom-bar").offset().top;
$(document).scroll(function () {
if ($(window).scrollTop() >= navBarY) {
$(".bottom-bar").addClass("fixed-top");
} else {
$(".bottom-bar").removeClass("fixed-top");
}
});
});
Whenever the .bottom-bar reaches the top of the window a class fixed-top is added.
CSS
.fixed-top {
position: fixed;
margin-top: 0 !important;
}
Example
$(document).ready(function () {
var navBarY = $(".bottom-bar").offset().top;
$(document).scroll(function () {
if ($(window).scrollTop() >= navBarY) {
$(".bottom-bar").addClass("fixed-top");
} else {
$(".bottom-bar").removeClass("fixed-top");
}
});
});
body {
margin: 0px;
font-family: Helvetica, Arial;
height: 2000px;
}
.bottom-bar {
width: 100%;
height: 40px;
background: #2A2A2A;
color: white;
text-align: center;
line-height: 40px;
margin-top: calc(100vh - 40px);
}
.fixed-top {
position: fixed;
margin-top: 0 !important;
}
<nav class="bottom-bar">Navigation bar</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

Div inside scroll area being scrolled away

I have a Scrollable area and buttons < > to make it scroll.
I need to have those button's span inside the main div (spinAreaDiv) without they getting scrolled away.
Q: What am I missing? (fiddle here)
I tried having the span with float, then they are inside spinAreaDivbut get scrolled away...
HTML
<div id="spinAreaDiv">
<span id="leftclick" class="left"> < </span>
<span id="rightclick" class="right"> > </span>
<div class="spin-bullets">
zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...
zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...
zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...
zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...
zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...zzzZZzzz...
</div>
JS:
spinAreaDiv = document.getElementById('spinAreaDiv');
scrollEff = new Fx.Scroll(spinAreaDiv, {
wait: false,
duration: 1000,
offset: {
'x': 0,
'y': 0
},
transition: Fx.Transitions.Quad.easeInOut
});
thumbLeft = document.getElementById('leftclick');
thumbRight = document.getElementById('rightclick');
thumbLeft.addEvent("click", function () {
scrollEff.start((spinAreaDiv.getScroll().x) - 400, 0);
});
thumbRight.addEvent("click", function () {
scrollEff.start((spinAreaDiv.getScroll().x) + 400, 0);
});
CSS:
#spinAreaDiv {
width:500px;
margin-left:10%;
overflow:auto;
height:60px;
background-color:grey;
}
span {
margin-top:20px;
background-color:red;
z-index:100;
}
span.right {position:absolute;right:0px;}
span.left {position:absolute;left: 0;}
.spin-bullets {
width:10000px;
height:35px;
padding-top:10px;
top:0px;
background-color:#066;
}
(Using Mootools Fx.Scroll)
I'm not familiar with "MooTools", but you could change the left/right offset on the buttons during scroll:
spinAreaDiv.addEvent('scroll', function(ev){
thumbLeft.style.left = spinAreaDiv.getScroll().x + 'px';
thumbRight.style.right = (-spinAreaDiv.getScroll().x) + 'px';
});
(test)
Also, you need to position your spinArea:
#spinAreaDiv {
position: relative;
}
It might be a better idea to move your scroll content into another DIV with the scroll width and height, and put the buttons there. Example.
Your span buttons should be standing outside of #spinAreaDiv or take reference of position from a parent that do not scroll.
http://jsfiddle.net/53AMT/1/.
if you wrap your slider in :anther div;
#myslider {
position:relative;
z-index:1;
width:500px;
margin:auto;
}
They can lay over it though.