how to position a scroll_to_top button - html

in the example below how to keep gotop inside story - i.e. bottom right of the middle div
and keep its show/hide functionality
the three divs - top, story and footer - are not of predictive width and height
I tried various position and margin params - without success
$(document).on('scroll', function(){
let x = $(this).scrollTop();
if(x > 25){$('.gotop').show();}
else{$('.gotop').hide()}
});
$('.gotop').on('click', function(){
$(document).scrollTop(0);
});
.container{
width:50%;
margin:0 auto;
background:silver;
text-align:center;
}
.top{padding:25px; background:gold;}
.story{
position:relative;
padding:25px;
min-height:100vh;
}
.footer{padding:25px; background:gold;}
.gotop{
display:none;
position:fixed;
z-index:99;
right:14px;
bottom:0;
cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='container'>
<div class='top'>TOP</div>
<div class='story'><br><br><br><br>STORY<br><br><br>
<div class='gotop'>gotop</div>
</div>
<div class='footer'>FOOTER</div>
</div>

Use sticky position:
$(document).on('scroll', function() {
let x = $(this).scrollTop();
if (x > 25) {
$('.gotop').show();
} else {
$('.gotop').hide()
}
});
$('.gotop').on('click', function() {
$(document).scrollTop(0);
});
.container {
width: 50%;
margin: 0 auto;
background: silver;
text-align: center;
}
.top {
padding: 25px;
background: gold;
}
.story {
position: relative;
padding: 25px;
min-height: 150vh;
/* I need flexbox to push the button to bottom*/
display:flex;
flex-direction:column;
}
.footer {
padding: 25px;
background: gold;
}
.gotop {
display: none;
position: sticky;
margin-top:auto; /* push to bottom */
text-align:right; /* right align */
z-index: 99;
right: 14px;
bottom: 14px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='container'>
<div class='top'>TOP</div>
<div class='story'><br><br><br><br>STORY<br><br><br>
<div class='gotop'>gotop</div>
</div>
<div class='footer'>FOOTER</div>
</div>

In the following implementation, I have taken some liberty to change minor css , replace jquery with js by introducing Intersection Observer which will only make the gotop element visible when our footer starts getting visible. Ofcourse you can use both jquery and js simultaneously but since I don't have experience with former, I used js only.
The benefit is that it's not dependent on any hardcoded values to control gotop visibility.
let footer = document.querySelector('.footer');
let gotop = document.querySelector('.gotop');
let observer = new IntersectionObserver((entries)=>{
entries.forEach(entry=>{
if(entry.isIntersecting){
gotop.style.opacity = '1';
gotop.style.pointerEvents = 'auto';
}
else{
gotop.style.opacity = '0';
gotop.style.pointerEvents = 'none';
}
})
})
observer.observe(footer);
gotop.addEventListener('click',()=>window.scrollTo({top:true}));
.container{
width:50%;
margin:0 auto;
background:silver;
text-align:center;
}
.top{padding:25px; background:gold;}
.story{
position:relative;
padding:25px;
min-height:100vh;
}
.footer{padding:25px; background:gold;}
.gotop{
opacity:0;
pointer-events:none;
position:absolute;
z-index:99;
right:14px;
bottom:0;
cursor:pointer;
transition:all 0.2s ease-in;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='container'>
<div class='top'>TOP</div>
<div class='story'><br><br><br><br>STORY<br><br><br>
<div class='gotop'>gotop</div>
</div>
<div class='footer'>FOOTER</div>
</div>

You only need to change position:fixed; to position:absolute; for your .gotop div. Since it is positioned inside a position: relative div then it will be inside of it.
$(document).on('scroll', function(){
let x = $(this).scrollTop();
if(x > 25){$('.gotop').show();}
else{$('.gotop').hide()}
});
$('.gotop').on('click', function(){
$(document).scrollTop(0);
});
.container{
width:50%;
margin:0 auto;
background:silver;
text-align:center;
}
.top{padding:25px; background:gold;}
.story{
position:relative;
padding:25px;
min-height:100vh;
}
.footer{padding:25px; background:gold;}
.gotop{
display:none;
position:absolute;
z-index:99;
right:14px;
bottom:0;
cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='container'>
<div class='top'>TOP</div>
<div class='story'><br><br><br><br>STORY<br><br><br>
<div class='gotop'>gotop</div>
</div>
<div class='footer'>FOOTER</div>
</div>

Related

How to make this div not past through the other one on the right side?

I have this problem, I can't seem to figure out how do I make this red DIV not past through the div on the right side, I want to make it stay between the right and the left div menu.
Website Screenshot
HTML Code:
<body>
<div class="navigationBar_default">
<div class="facetubeLogo_default"></div>
<div class="facetubeText_container_default"><center><strong class="facetubeText_default"></strong></center></div>
</div>
<div class="content_default">
<div class="leftMenu_onProfile">
<button class="leftMenu_buttonOnProfile"><img class="leftMenu_buttonImage" src="images/myProfileButton_onLeftMenu_onProfile.png" /><br>MY PROFILE</button>
<button class="leftMenu_buttonOnProfile"><img class="leftMenu_buttonImage" src="images/optionsButton_onLeftMenu_onProfile.png" /><br>GENERAL</button>
<button class="leftMenu_buttonOnProfile"><img class="leftMenu_buttonImage" src="images/generalButton_onLeftMenu_onProfile.png" /><br>OPTIONS</button>
<button class="leftMenu_buttonOnProfile"><img class="leftMenu_buttonImage" src="images/supportUsButton_onLeftMenu_onProfile.png" /><br>SUPPORT US</button>
<button class="leftMenu_buttonOnProfile"><img class="leftMenu_buttonImage" src="images/logOutButton_onLeftMenu_onProfile.png" /><br>LOG OUT</button>
</div>
<!-- there must be some stuff here, otherwise leftMenu_onProfile will glitch out. -->
<div class="contentCenter_onProfile">
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAA
</div>
<!-- there must be some stuff here, otherwise leftMenu_onProfile will glitch out. -->
<div class="rightMenu_onProfile">user1<br>user2<br>user3<br>user4<br>user5</div>
</div>
CSS Code:
.navigationBar_default {
background-color:#202020;
width:100%;
height:32px;
margin-left:-8px;
margin-right:-8px;
border:0;
top:0;
position:fixed;
}
.facetubeLogo_default {
background-image:url("../images/facetube_icon_30x30_png.png");
width:30px;
height:30px;
margin-top:1px;
margin-left:1px;
float:left;
}
.facetubeText_container_default {
margin-top:6px;
}
.content_default {
width:100%;
height:100%;
margin-top:32px;
margin-left:-8px;
margin-right:-8px;
margin-bottom:-8px;
}
.leftMenu_onProfile {
background-color:#0d0d0d;
margin-top:0px;
margin-left:-8px;
width:240px;
float:left;
}
.leftMenu_buttonOnProfile {
background-color:#0d0d0d;
border:0;
border-bottom:1px solid #ffffff;
width:240px;
height:48px;
color:#ffffff;
}
.leftMenu_buttonImage {
float:left;
margin-top:-2px;
margin-right:-6px;
}
.contentCenter_onProfile {
background-color:red;
padding:2px; /* padding is originally removed */
margin:0px;
top:0px;
}
.rightMenu_onProfile {
background-color:#0d0d0d;
margin-top:-20px;
margin-right:-16px;
width:300px;
color:#ffffff;
float:right;
}
I have feeling it has to do something with content_default and the width:100% but I don't know. I tried everything and I couldn't figure it out.
:/
Try this:
.contentCenter_onProfile {
background-color:red;
padding:2px; /* padding is originally removed */
margin:0 300px;
top:0px;
width: 50%; /* SET THIS */
white-space: pre-wrap;
word-break: break-all;
}
By setting the width, you can ensure the div does not overlap the right hand side div.
Setting float: left or float: right causes the elements to overlap.

Footer element being offset by other elements

I have been messing with this for hours and cannot figure out what the problem is.
I have a basic Local Weather App I've created for practice. When I add my footer to the bottom of the page it is automatically offset to the right from the center. Regardless of the position property set, I cannot use text-align:center or margin:auto to center the element.
HTML:
<div id='content-box'>
<div id='weather'>
<p id='temp'></p>
<p id='humidity' class='other'></p>
<p id='wind' class='other'></p>
<img id='icon' src=''>
</div>
<p id='location'></p>
</div>
<footer>FOOTER</footer>
CSS:
#content-box{
position:relative;
text-align:center;
width:33%;
margin:auto;
height:350px;;
background-color:rgba(0,0,0,0.1);
border-radius: 35px;
}
#weather{
font-size:3em;
}
#temp{
position:relative;
bottom:50px;
float:left;
font-size:3em;
font-family:Arial;
padding-left:20px;
color:#329555;
}
#temp:hover{
opacity:.8;
cursor:pointer;
}
span{
font-family:'Raleway', sans-serif;
}
.other{
font-size:.5em;
width:auto;
text-align:right;
padding: 10px 20px 0px 0px;
font-family:sans-serif;
}
#location{
position:absolute;
bottom:0px;
width:100%;
font-size:1.7em;
letter-spacing: 8px;
font-family:'Raleway';
}
#icon{
position:relative;
padding-top:10px;
display:flex;
float:right;
bottom:40px;
width:175px;
}
footer{
position:relative;
margin-top:50px;
}
My code is also on codepen, so it may be easiest for one to actually see the bug in action.
On the click event to convert Fahrenheit to Celsius it is also pushing the footer to the right, for which I am also lost as to why this is.
CODEPEN example,
What I had changed is,
footer{
position: relative;
clear:both;
}
and removed all the margins and paddings in body,
html, body{
height:100%;
margin:0;
padding:0;
}
Now you can even text align in your footer section too
footer{
position: relative;
clear:both;
text-align:center;
}
CODEPEN example for text-align
You can try this:
<footer>
<div style="
margin: 0 auto;
width: fit-content;
width: -webkit-fit-content;
">Code By Michael Ryan</div>
</footer>
It worked for me.
use clear: both;
The clear CSS property specifies whether an element can be next to
floating elements that precede it or must be moved down (cleared)
below them. The clear property applies to both floating and
non-floating elements.
var main = function(){
function getLocation(){
$.get('http://ip-api.com/json', function(loc){
$('#location').html(loc.city + ", " + loc.region + " " + loc.zip);
getWeather(loc.lat, loc.lon);
});
}
function getWeather(lat,lon){
var url = 'http://api.openweathermap.org/data/2.5/weather?lat=' +
lat + '&lon=' + lon + '&units=imperial' + '&type=accurate' +
'&APPID=ab4b9ad58133c89326de9f6ae59d7b66';
$.get(url, function(weatherInfo){
var temp = Math.round(weatherInfo.main.temp),
tempC = Math.round((temp - 32) * 5/9),
humidity = weatherInfo.main.humidity,
wind = weatherInfo.wind.speed,
icon = 'http://openweathermap.org/img/w/' + weatherInfo.weather[0].icon +'.png';
$('#temp').html(temp + '°<span>F</span>');
$('#humidity').html('Humidity: ' + humidity + '%');
$('#wind').html('Wind Speed: ' + wind + 'mph');
$('#icon').attr('src', icon);
$('#temp').click(function(){
if ($('#temp').text().indexOf('F') !== -1)
$('#temp').html(tempC + '°<span>C</span>');
else
$('#temp').html(temp + '°<span>F</span>');
})
});
}
getLocation();
}
$(document).ready(main);
html{
background:url('http://g01.a.alicdn.com/kf/HTB14WXBJVXXXXaSXFXXq6xXFXXXQ/Hot-Selling-Vinyl-4x5ft-Backdrop-Sunshine-Sea-Flowers-Photography-New-Portrait-Digital-Wedding-Backgrounds-1-25.jpg_640x640.jpg');
background-repeat:no-repeat;
background-size:cover;
}
body{position:relative;}
html, body{
height:100%;
}
header{
text-align:center;
font-size:2em;
letter-spacing:9px;
font-family:'Raleway', sans-serif;
}
h1{
opacity:0.45;
}
#content-box{
position:relative;
text-align:center;
width:33%;
margin:auto;
height:350px;;
background-color:rgba(0,0,0,0.1);
border-radius: 35px;
}
#weather{
font-size:3em;
}
#temp{
position:relative;
bottom:50px;
float:left;
font-size:3em;
font-family:Arial;
padding-left:20px;
color:#329555;
}
#temp:hover{
opacity:.8;
cursor:pointer;
}
span{
font-family:'Raleway', sans-serif;
}
.other{
font-size:.5em;
width:auto;
text-align:right;
padding: 10px 20px 0px 0px;
font-family:sans-serif;
}
#location{
position:absolute;
bottom:0px;
width:100%;
font-size:1.7em;
letter-spacing: 8px;
font-family:'Raleway';
}
#icon{
position:relative;
padding-top:10px;
display:flex;
float:right;
bottom:40px;
width:175px;
}
footer{
position:relative;
margin-top:50px;
clear:both;
}
footer .fix{
text-align: center;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
</head>
<body>
<header>
<h1>Local Weather App</h1>
</header>
<div id='content-box'>
<div id='weather'>
<p id='temp'></p>
<p id='humidity' class='other'></p>
<p id='wind' class='other'></p>
<img id='icon' src=''>
</div>
<p id='location'></p>
</div>
<footer><div class="fix">Code By Michael Ryan</div></footer>
</body>
</html>

How to display a button over a picture only when hovering it?

I'm trying to reproduce some pieces of CSS from the http://flink.to website, especially the tiles which contains for each article the picture, the title, the author, the link to the author page and the link to the article.
Here is the HTML for one tile :
<div class="block-module">
<a href="http://flink.to/stories/54b6e61de3039db33f00000b" class="article-link">
<span class="button">View Story</span>
</a>
<img src="https://cdn01.flink.to/api/image/54f492ec30323921c9000000/300/300/fill">
<div class="block-about">
<h2 class="block-title">Arch Enemy’s Perpetual Revolution</h2>
<span class="block-stats">
by Andrew Epstein
</span>
</div>
</div>
Here is the CSS for one tile :
.block-module { width: 283px; height: 283px; font-size: 0.9622em; display: block; cursor:pointer; border-radius:0.3125em; overflow:hidden; z-index:4; position:relative; }
.block-about { position:absolute; bottom:0; left:0; right:0; padding:4em 1em 1em 1em; background-image:-webkit-linear-gradient(transparent, rgba(0,0,0,0.55), rgba(0,0,0,0.8)); background-image:linear-gradient(transparent, rgba(0,0,0,0.55), rgba(0,0,0,0.8)); }
.block-about a { position:relative; z-index:5; }
.block-title { max-width:100%; margin:0 0 0; color: white !important;font-size:1.625em; }
.block-stats { width:100%; margin-top:0.35714em; font-size:0.875em; color:rgba(255,255,255,0.55) !important; }
.button { color:#ffffff; background-color:#337d94; }
.author-link { color:#659dae; }
Everything's OK except that we can't access the article and the "view story" link which is supposed to show up only when we hover the picture, in the middle/center of it.
Edit : Here is a demo : http://jsfiddle.net/5qwejk20/
As the website's CSS sheet of Flink.to is really very complicated, I didn't find how to resolve this. Could you please help me ?
There is a lot of CSS, and obviously it's hard to tell what does what and it will need to be trimmed. But from what I can tell these are the styles making it happen. The button opacity is initially 0 (hidden), so needed to change to 1.
JSFiddle
I added this style to make it show with the cursor
.view-full-module.mod-custom-icon:hover .button.view-full-custom-el {
opacity: 1;
}
By looking at the css the elements are hiding and showing by using the z-index property and CSS Positioning. Try the following code, I use different values of z-index to overlap elements. Remember that the z-index property only is valid for elements with position:absolute,position:relative or position:fixed so you have to scaffold your website having this on mind. I also added an id to the img to select it on the css. http://jsfiddle.net/cfahhmkj/
HTML
<div class="block-module">
<a href="http://flink.to/stories/54b6e61de3039db33f00000b" class="article-link">
<span class="button">View Story</span>
</a>
<img class="albumImage" src="https://cdn01.flink.to/api/image/54f492ec30323921c9000000/300/300/fill">
<div class="block-about" >
<h2 class="block-title">Arch Enemy’s Perpetual Revolution</h2>
<span class="block-stats">
by Andrew Epstein
</span>
</div>
</div>
CSS
.block-module { width: 283px; height: 283px; font-size: 0.9622em; display: block; cursor:pointer; border-radius:0.3125em; overflow:hidden; z-index:4; position:relative; }
.block-about { position:absolute; bottom:0; left:0; right:0; padding:4em 1em 1em 1em; background-image:-webkit-linear-gradient(transparent, rgba(0,0,0,0.55), rgba(0,0,0,0.8)); background-image:linear-gradient(transparent, rgba(0,0,0,0.55), rgba(0,0,0,0.8)); }
.block-about a { position:relative; z-index:5; }
.block-title { max-width:100%; margin:0 0 0; color: white !important;font-size:1.625em; }
.block-stats { width:100%; margin-top:0.35714em; font-size:0.875em; color:rgba(255,255,255,0.55) !important; }
.button { color:#ffffff; background-color:#337d94; }
.author-link { color:#659dae; }
.article-link {
position:absolute;
left:110px;
top: 120px;
z-index:-1;
}
.albumImage{
position:absolute;
z-index:0;
}
.albumImage:hover{
z-index:-2;
}

Stick header after scrolling

I made a header navigation and it's halfway down the page. When you scroll, and it's on the top of the page I want it to stick, if you know what I mean.
I hope someone can tell me how I get this done. Demo JsFiddle
HTML
<header>menu</header>
CSS
body, html {
height: 2000px;
}
header {
position: absolute;
top: 300px;
background-color: black;
color: white;
width: 100%;
}
You are going to need some JavaScript.
Demo JsFiddle
HTML
<header>
<div class="logo">AWESOME HEADER!</div>
<div class="menu">Menu goes here - home - links - blah blah</div>
</header>
<div class="content">
<!-- your stuff -->
</div>
CSS
* { margin:0; padding:0; }
.logo {
font-size:40px;
font-weight:bold;
color:#a00;
font-style:italic;
}
.menu {
background:#a00;
color:#fff;
height:30px;
line-height:30px;
letter-spacing:1px;
width:100%;
}
.content { margin-top:10px; }
/* the trick */
.menu-padding { padding-top:40px; }
.sticky {
position:fixed;
top:0;
}
Javascript (JQuery)
$(document).ready(function () {
var menu = document.querySelector('.menu');
var origOffsetY = menu.offsetTop;
function scroll() {
if ($(window).scrollTop() >= origOffsetY) {
$('.menu').addClass('sticky');
$('.content').addClass('menu-padding');
} else {
$('.menu').removeClass('sticky');
$('.content').removeClass('menu-padding');
}
}
document.onscroll = scroll;
});
Take a look at this tutorial. It explains how to do it using either CSS or jQuery depending on what you want.
http://www.hongkiat.com/blog/css-sticky-position/

Multiple Popups with HTML/CSS

I would like to have a series of items within a flex container, that when clicked, trigger a popup. However, it appears that each item involves at least one function. Is it possible to have multiple popups with one function? Many thanks.
.flex-image {
width: 22.5%;
height: 195px;
margin: 10px;
font-size: auto;
padding: 85px;
background-size: cover;
position: relative;
display: inline-block;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
<div class="flex-image" onclick="myFunction()" style = "background-
image:url(Images/Sample1.jpg);"> <span class="popuptext" id="myPopup"><img
class = "intimage" src= "Images/Sample1.jpg"> </span> </div>
<div class="flex-image" onclick="myFunction()" style = "background-
image:url(Images/Sample2.jpg);"> <span class="popuptext" id="myPopup"><img
class = "intimage" src= "Images/Sample2.jpg"> </span></div>
<script>
function myFunction() {
var popup = document.getElementById("myPopup");
popup.classList.toggle("show");
}
function myFunction1() {
var popup = document.getElementById("myPopup1");
popup.classList.toggle("show");
}
...
</script>
Is there a way to have it open the user's choice of popup without having to create a new function every time?
<!DOCTYPE html>
<html>
<head>
<style>
.box{
border:5px solid red;
background:#000;
padding:50px;
height:150px;
width:320px;
cursor:pointer;
margin-bottom:30px;
}
.box>h2{
color:#fff;
text-align:center;
}
.popup{
border:5px solid #000;
background:red;
padding:50px;
height:150px;
width:70%;
cursor:pointer;
position:fixed;
top:200px;
left:50px;
display:none;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".box").on('click', function(){
$(".popup").show();
});
$(".close").on('click', function(){
$(".popup").hide();
});
});
</script>
</head>
<body>
<div class="box">
<h2>1</h2>
</div>
<div class="box">
<h2>2</h2>
</div>
<div class="box">
<h2>3</h2>
</div>
<div class="popup">
<a href="#" class="close">close<a>
</div>
</body>
</html>