This question already has answers here:
Hide div when user reaches the bottom of page
(3 answers)
Closed 1 year ago.
I have a fixed image
<img id="project-badge" src=x">
with this CSS:
#project-badge {
position: fixed;
right: 40px;
z-index: 2;
max-width: 130px;
bottom: 65px;
display: block !important;
}
The image stays fixed on the right side as the user scrolls down the page.
Im trying to get it to disappear once it's about 50-100px from the bottom of the screen. Some sort of smooth transition disappear would be great too so it's not so sudden.
Can it be done with pure CSS, and if not, how can it be done with Javascript?
$(window).scroll((function() {
// when you have multiple element to disappear
$(this).scrollTop> x && ("your_identy_element").each(function(a){
// effect disappear smooth timeout
setTimeout((function() {
$("your_identy_element").eq(a).addClass("your_styling_disappear")
}), 650 * (a + 1))
})
}
))
maybe what you mean is the paralax landing element effect, I want to give a direct example of the code but I'm too lazy to code and it's quite a hassle, so I gave the youtube tutorial link about paralax landing element "https://www.youtube.com/watch?v=cEkCIn4rY4Q"
but it's language in indonesia, iam suggest you for watch until end and try.
as far as I know, there is no way to do this using CSS however it can be done with javascript using the code below.
window.onscroll = function(event) {
if ((window.innerHeight + Math.ceil(window.pageYOffset)) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
//you can add a css class to you're image element
}
};
You can do this without the overhead of listening and reacting to scroll by using IntersectionObserver.
Plant a 1px element 200px up from the bottom of the content (or wherever you want the badge to start disappearing), observe it so when it is in the viewport the badge fades away and when it leaves the viewport (i.e. the user scrolls up again) fade it in.
let observer = new IntersectionObserver(
(entries, observer) => {
entries.forEach(entry => {
const badge = document.querySelector("#project-badge");
if (entry.isIntersecting) { badge.classList.remove("fadein"); }
else { badge.classList.add("fadein"); }
});
});
observer.observe(document.querySelector("#pixel"));
#project-badge {
position: fixed;
right: 40px;
z-index: 2;
max-width: 130px;
bottom: 65px;
display: block !important;
/* ADDED */
opacity: 0;
transition: opacity 1s;
}
#project-badge.fadein {
opacity: 1;
}
#pixel {
position: absolute;
width: 1px;
height: 1px;
top: calc(100% - 200px);
}
/* JUST FOR THE DEMO */
#content {
background: linear-gradient(to bottom, white, gray);
position: relative;
height: 200vh;
}
#project-badge {
background-image: linear-gradient(to right, cyan, lime);
height: 30px;
width: 130px;
}
<img id="project-badge" src="x">
<div id="content">CONTENT - SCROLL DOWN AND UP TO SEE BADGE FADE OUT AND IN
<div id="pixel"></div>
</div>
Related
I am wondering if there is a style that keeps the text within a div locked into a straight line no matter what the width of the container is? (No stacked text)
See I have a slide-in menu that is working nicely, the buttons look great, but there's just one thing that's visually offputting and it's that the text is stacked up as it slides out and then flattens out as the width of the container is increased to full screen.
Here's the HTML of a menu item:
<div class="navigation-menu-slide-in-10">
Account
</div>
And here's the CSS of the menu item:
.navigation-menu-slide-in-10 {
width: 100%;
height: 6vh;
background-color: transparent;
display: flex;
justify-content: left;
align-items: center;
font-family: bahnschrift;
font-size: 28px;
font-weight: bold;
}
.navigation-menu-slide-in-10 a {
padding-left: 24px;
text-decoration: none;
color: #ffffff;
}
As for how the menu unfolds, here's the Javascript. The button is clicked and then goes out of visual existence. Then the other button is made visible within my slide-out-menu. Then the slide-out-menu is made visible. And then is increased to 100% width with a transition delay which acts as an animation.
function mobileNavigationButtonOn() {
document.getElementById("mobileNavigationButtonOff").style.display = "none";
document.getElementById("mobileNavigationButtonOn").style.display = "flex";
document.getElementById("mobileNavigationMenuSlideIn").style.visibility = "visible";
document.getElementById("mobileNavigationMenuSlideIn").style.width = "100%";
}
Oh, and I guess the CSS for the slide-in-menu might be pertinent too. Here is that:
.navigation-menu-slide-in {
width: 0%;
height: 100%;
background-image: linear-gradient(to right, #ff3300, #ff7700);
position: fixed;
visibility: hidden;
transition-duration: 0.6s;
transition-property: width;
overflow-x: hidden;
z-index: 3;
}
Yeah, so I'm looking for a way to keep the text fixed into a straight line. Thanks.
I found the style that works if anyone is having this same issue...
white-space: nowrap;
Thanks for taking the time to read my post.
So I've started freelancing recently (CSS and HTML) and I've found my first difficulty.
Look at the green bar (Its a fixed div), its green for testing porpuses, but client wants it to be transparent when on top of this orange background...
...But switch to another color when on top of this white background (So the letters can be seen)
Is this possible to do with CSS? If so, how do I do it?
Thanks again!
Pure css does not currently have any amount of responsiveness to what is and is not onscreen. So, the short lame answer is "not with just css."
That being said, it's very easy to do this with js.
The event you'll be looking for is scroll event.
From there you can add/remove a class for styling.
Something like this:
// wait for document.addEventListener("DOMContentLoaded");
const myHeader = document.getElementById("MyHeader");
window.addEventListener("scroll", () => {
const scrollPos = window.scrollY;
if (scrollPos ... add your logic here) {
myHeader.classList.add("scrollIsThing"); // this is the css class you'll target
} else {
myHeader.classList.remove("scrollIsThing");
}
});
Sorry there's no good css way to do this.
It is "kind of" possible to do this just with css if you can accept some html markup duplication. You can split the fixed header into two layers, one for white text, one for background, and using z-index you can sandwich the content between these two header layers (in case of a colorful content), in which case only the white text would be visible, and position the white content below the header background. A sample of such behavior is shown below:
.header {
height: 50px;
background: transparent;
position: fixed;
top: 0;
width: 100%;
color: white;
font-family: sans;
text-align: center;
line-height: 50px;
z-index: 3;
}
.header.header-background {
background: teal;
z-index: 1;
}
.body1 {
height: 120vh;
background: orange;
z-index: 2;
position: relative;
}
.body2 {
height: 120vh;
background: #eee;
}
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
<div class="header">
White text
</div>
<div class="body1"></div>
<div class="header header-background">
</div>
<div class="body2"></div>
I am in NO SHAPE or form a coder, but have made some tweaks to my BigCartel site that is expected to come out in the next few weeks. I have a clothing line, and I wanted enable consumers who have selected a product, to be able to hover over the image of the product to view it magnified... (here is an example from Nike of what I mean: http://store.nike.com/us/en_us/pd/breathe-womens-short-sleeve-running-top/pid-11319700/pgid-11619220 ) I wanted to know what code to use to make the image/product that a consumer has clicked on and is viewing larger/magnify when hovering over a certain area... I saw some codes uploaded, but SINCE I am not a professional coder, I was wondering WHERE to insert it in the custom coding . I have a CSS option, and HTML and I don't know if I should go to "Products" or the over all coding...(Sorry for the rookie question)...
I also want to know (If I can slide this question in there as well) How to speed up the speed of the slide show on my BigCartel site, and possibly even change it to a dissolve option... And, again, where would I insert that code..
I've made some minor changes on my own, but again, I am NO CODER and there are a few additional tweaks, I would love to make to not make my site so "cookie cutter" The good folks at BigCartel, sent me this link to search and ask questions on. Thanks so much in advance for your help!
Have you tried this this always works for me https://codepen.io/ccrch/pen/yyaraz
JS
$('.tile')
// tile mouse actions
.on('mouseover', function(){
$(this).children('.photo').css({'transform': 'scale('+ $(this).attr('data-scale') +')'});
})
.on('mouseout', function(){
$(this).children('.photo').css({'transform': 'scale(1)'});
})
.on('mousemove', function(e){
$(this).children('.photo').css({'transform-origin': ((e.pageX - $(this).offset().left) / $(this).width()) * 100 + '% ' + ((e.pageY - $(this).offset().top) / $(this).height()) * 100 +'%'});
})
// tiles set up
.each(function(){
$(this)
// add a photo container
.append('<div class="photo"></div>')
// some text just to show zoom level on current item in this example
.append('<div class="txt"><div class="x">'+ $(this).attr('data-scale') +'x</div>ZOOM ON<br>HOVER</div>')
// set up a background image for each tile based on data-image attribute
.children('.photo').css({'background-image': 'url('+ $(this).attr('data-image') +')'});
})
HTML
<div class="tiles">
<div class="tile" data-scale="1.1" data-image="http://ultraimg.com/images/0yS4A9e.jpg"></div>
<div class="tile" data-scale="1.6" data-image="http://ultraimg.com/images/hzQ2IGW.jpg"></div>
<div class="tile" data-scale="2.4" data-image="http://ultraimg.com/images/bNeWGWB.jpg"></div>
</div>
CSS
#import url(http://fonts.googleapis.com/css?family=Roboto+Slab:700);
body {
background: #fff;
color: #000;
margin: 0;
}
.tiles {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.tile {
position: relative;
float: left;
width: 33.333%;
height: 100%;
overflow: hidden;
}
.photo {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-position: center;
background-size: cover;
transition: transform .5s ease-out;
}
.txt {
position: absolute;
z-index: 2;
right: 0;
bottom: 10%;
left: 0;
font-family: 'Roboto Slab', serif;
font-size: 9px;
line-height: 12px;
text-align: center;
cursor: default;
}
.x {
font-size: 32px;
line-height: 32px;
}
I am trying to have 2 divs totally overlapping: one div for a menu controller, one div for a menu.
The menu controller will catch all the mouse events, make the menu div disappear, etc.
I have settled most of it already, but I cannot manage to have the controller div on top of the menu div. To check the positioning, I change the background color of the controller in order to see if the menu is hidden.
My code is at the following location: http://codepen.io/3MO/pen/mJKeKg.
The main idea is the following:
#menu {
z-index: 0;
top: 0px;
position: absolute;
width: 100%;
height: 150px;
background: linear-gradient(90deg, rgb(60, 60, 60) 40%, white 49%, white 51%, rgb(60, 60, 60) 60%);
background-repeat: no-repeat;
background-clip: text;
text-fill-color: transparent;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 16px;
cursor: auto;
}
#menuController {
z-index: 10;
top: 0px;
position: absolute;
width: 100%;
height: 150px;
background-color: red;
}
I tried position:relative and position: absolute for both divs, no luck so far. Can you tell me what I do wrong?
Thanks!
You should add position: relative to their parent ( <body> ) element
I've managed to create a workaround this issue using javascript since you're already using this a lot. Function you need is below. I'm hiding menuController just for a moment, check what is beneath it under click coordinates and then showing it again.
function registerControllerClick() {
$(menuController).click(function(event) {
$(menuController).hide();
window.location.href = document.elementFromPoint(event.clientX, event.clientY).href;
$(menuController).show();
});
}
Add it to your init code:
function init() {
registerControllerOver();
registerControllerOut();
registerControllerClick();
}
Working example
If you don't want to go to location specified by href, but trigger click event instead use jQuery trigger('click') - take a look here
I have found this code from google, and it is working fine but the pop up window is transparent and I want to make it opaque. (I know that the pop up window is transparent because if I zoom in the browser I can see the pop up window overlapping with the background content and the background content is visible)
Here is the link to code:
popup window
<HTML>
<HEAD>
<TITLE>Popup div with disabled background</TITLE>
<style>
.ontop {
z-index: 9999;
width: 100%;
height: 100%;
top: 0;
left: 0;
display: none;
position: absolute;
background-color: #cccccc;
color: #aaaaaa;
opacity: .8;
filter: alpha(opacity = 80);
}
#popup {
width: 300px;
height: 200px;
position: absolute;
color: #000000;
background-color: #ffffff;
/* To align popup window at the center of screen*/
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -150px;
}
</style>
<script type="text/javascript">
function pop(div) {
document.getElementById(div).style.display = 'block';
}
function hide(div) {
document.getElementById(div).style.display = 'none';
}
//To detect escape button
document.onkeydown = function(evt) {
evt = evt || window.event;
if (evt.keyCode == 27) {
hide('popDiv');
}
};
</script>
</HEAD>
<BODY>
<div id="popDiv" class="ontop">
<table border="1" id="popup">
<tr>
<td>
This is can be used as a popup window
<br></br>
Click Close OR escape button to close it
Close
</td>
</tr>
</table>
</div>
<CENTER>
<h3>
Simple popup div with disabled background
</h3>
<br/>
Click here to open a popup div
</CENTER>
</BODY>
For some reason, I am not sure of, I couldn't make this code work in jsfiddle, but I have used the same code in one html file with tags for css and it is working fine.
Kindly help.
It works, but only if the Javascript comes before your onclick-handlers in the source code.
So you need to change the following setting in JSFiddle (the second dropdown must be set to "No wrap - in <head>":
In the updated fiddle, I also fixed the opacity issue. Your whole overlay had opacity: 0.8; and that affects also all children of that overlay. Instead, you should use slightly transparent background-color in rgba notation for overlay:
background-color: rgba(204,204,204,0.8);
https://jsfiddle.net/ppqct0dg/4/
rgba uses decimal number, in contrast to #cccccc notation, which uses hexa-decimal numbers.