How to show/hide div with javascript? - javascript

So I want to hide a div for user settings, which shows when the user clicks on a little cog icon. What is the best way for me to go about this?
I made a couple of very basic pics to show what I'm after:
Closed:
Open:
Once the user clicks the icon (the red dot) then I want the hidden div to push the content to its right over. I had a look at the CSS checkbox hack, but that requires the user to click directly on the icon (the red dot) again to close the hidden div. I would like to have it so the user can click anywhere else on the page to close the div.
Anyone know how to do this with pure js?
Any help is greatly appreciated, thanks!

You can try tabindex="-1" in order to use :focus.
This way you can achieve it with pure CSS.
#wrapper {
display: flex;
position: fixed;
top: 0; right: 0; bottom: 0; left: 0;
}
#sidebar, #main, #hidden:focus {
padding: 10px;
}
#sidebar {
background: #A8D4AF;
}
#main {
flex-grow: 1;
background: #96A7B1;
}
#hidden {
width: 0;
background: #008DD2;
}
#hidden:before {
content: '';
display: block;
background: red;
border-radius: 50%;
position: relative;
left: -40px; top: 40px; /* Position of the dot*/
width: 20px; height: 20px; /* Size of the dot */
margin-bottom: -20px; /* Same as height */
}
#hidden:focus:before {
display: none;
}
#hidden:focus {
width: auto;
}
<div id="wrapper">
<div id="sidebar">
Test content
</div>
<div id="hidden" tabindex="-1">
Hidden stuff
</div>
<div id="main">
Lorem ipsum dolor
</div>
</div>

Related

Switch header from absolute to fixed positioned with slide in/out transition when scrolled down

I have a website header which appears full-size and is positioned absolutely such that it allows the underlying content to be partially visible 'behind' it. It should scroll off screen as the user scrolls down the page.
When the user hits a certain scroll point, 400px down the page, the header should re-appear in a fixed position at the top of the viewport and should be a smaller/minimal version. To facilitate this in a performant way (i.e not using window scroll events) I'm using an invisible marker and setting an IntersectionObserver on it to set a class on the body when the marker has been hit.
This works nicely, although please run this in a full-size viewer as the small inline version doesn't work so well (something I will address, but not part of the question):
let waypoint = new IntersectionObserver(entries => {
document.body.classList.toggle('waypoint-passed', !entries[0].isIntersecting);
});
waypoint.observe(document.querySelector('.waypoint'));
* {
margin: 0;
padding: 0;
}
.header {
position: absolute;
top: 0;
right: 0;
left: 0;
z-index: 10;
background-color: rgba(0,0,255,0.5);
color: #fff;
}
.header__full {
padding: 100px 0;
}
.header__min {
padding: 20px 0;
display: none;
}
body.waypoint-passed .header {
position: fixed;
}
body.waypoint-passed .header__full {
display: none;
}
body.waypoint-passed .header__min {
display: block;
}
.section {
aspect-ratio: 4 / 3;
background-color: #ddd;
color: #333;
display: grid;
place-items: center;
}
.section + .section {
margin-top: 10px;
}
.trigger-indicator {
border-top: 1px dashed red;
color: red;
position: absolute;
top: 400px;
right: 0;
left: 0;
}
.waypoint {
position: absolute;
top: 400px;
width: 0;
height: 0;
}
<header class="header">
<div class="header__full">Full Header</div>
<div class="header__min">Minimal Header</div>
</header>
<section class="section">Section 1</section>
<section class="section">Section 2</section>
<section class="section">Section 3</section>
<section class="section">Section 4</section>
<div class="trigger-indicator">Trigger Point</div>
<div class="waypoint"></div>
However, the last requirement is proving tricky! I need the minimal version of the header to slide down into position when the trigger point has been passed as the user scrolls down the page and then slide back up when the trigger point has been passed when the visitor scrolls back up.
The most performant way to do the animation is via CSS transform: translateY() so I'd like to implement it like that, but I just cannot figure out a nice way to trigger the slide down/up transitions.

How to make an element sticky to both bottom and top of page using CSS

I'd like to make an element sticky such that it "sticks" to both the top of the page if the user scrolls past it, and to the bottom of the page before the user scrolls to it.
I understand how to make it sticky in one way, but not both
I found the answer myself by slightly augmenting this solution
HTML (unchanged):
BEFORE
<div class="hold">
<div class="item">
THING
</div>
</div>
AFTER
CSS:
.hold {
position: relative;
margin-top: -2000px;
margin-bottom: -2000px; /* Added this line */
pointer-events: none;
}
.hold:before {
content: "";
display: block;
height: 2000px;
}
/* Added the following block */
.hold:after {
content: "";
display: block;
height: 2000px;
}
.item {
pointer-events: initial;
position: sticky;
top:0; /* Made this addition */
bottom: 0;
z-index: 100;
}

Z-Index of Material Dropdown component not layering underneath a fixed div when opened

Objective:
I would like the Header, Tab Section, and the Radio Button Section to be fixed in a form (see image below). Meaning that they should always be in view, and never have any overlapping elements.
The form looks like the following:
This is working fine when I simply scroll down on the form:
The Problem:
When I open the Angular Material dropdown, it overlaps over the Radio Button Section:
Here is the HTML. The highlighted sections are the elements that I want to be fixated on the form:
And here is the CSS for the 3 sections
//Header:
.module__header {
position: fixed;
top: 0px;
z-index: 1001;
display: flex;
height: 35px;
width: 100%;
background-color: #082749;
color: #FFFFFF;
font-size: 14px;
font-weight: 500;
align-items: center;
justify-content: stretch;
padding: 0;
margin-bottom: 0;
}
// Tab Section:
.mat-tab-label-container {
position: fixed;
top: 35px;
padding-top: 10px;
z-index: 1001;
width: 100%;
background: #fff;
}
// Radio Button Section:
.timaticFullTextView {
padding-top: 35px;
padding-left: 15px;
padding-bottom: 15px;
background: #fff;
z-index: 1001;
position: fixed;
width: 100%;
border-bottom: 1.5px solid gray;
}
I have tried changing the cdk-overlay-container to a z-index of <1001, but that still is overlapping the Radio Button Section.
How can I have the opened dropdown display underneath all 3 sections?
Edit: Adding screenshot to show the cdk-overlay that is giving issues. I have tried removing and lowering the z-index, but it doesn't have any effect
The problem is that mat-tab-body has z-index: 1 and this won't allow your fixed view inside to have a higher elevation. You can remove the z-index from mat-tab-body put then your content without a z-index won't be clickable anymore so you have to add a z-index and position to your not fixed content.
The code would have to look something like this:
<mat-tab>
<mat-tab-body> <!-- <-- added automatically -->
<div class="tab-header"></div>
<div class="tab-content"></div>
</mat-tab-body>
</mat-tab>
::ng-deep mat-tab-body {
z-index: unset !important;
}
.tab-header {
position: fixed;
z-index: 1001;
}
.tab-content {
position: relative;
z-index: 1;
}
You've found the right element whilst applying styles to the wrong one.
Here is how I made it work
.cdk-global-overlay-wrapper, .cdk-overlay-container {
z-index: 99999 !important;
}

set class in css or hover over 2 overlapping divs

Is it possible to give the hover-icon a class, so that the icon is the triggerinfo? The image is in gray when i hover it, it gets colored but I wan't to hover a text when is colored, when I going over the little icon. Is there a way overlapping the div with the triggerinfo class over the image, but not leaving the hover of the image. Like hover the div that is not visible and not leaving the hover effect colored ?
Thanks !
If it helps I can share the link to my website, but only as message not for the public post. It gets more visual, and I think better to understand what I mean.
jQuery(document).ready(function() {
jQuery(".triggerinfo").mouseleave(function() {
jQuery(this).next(".info").hide();
});
jQuery(".triggerinfo").hover(function() {
jQuery(this).next(".info").toggle("fade");
});
});
.info {
display: none;
padding: 10px;
background: #fff;
position: absolute;
box-sizing: border-box;
z-index: 1;
}
.triggerinfo {
display: inline-felx;
opacity: 0.1;
position: absolute;
margin-top: -50px;
margin-left: 30px;
z-index: 3;
}
.uk-overlay-icon:before {
content: "\f0c9";
position: absolute;
top: 90%;
left: 10%;
width: 30px;
height: 30px;
margin-top: -15px;
margin-left: -15px;
font-size: 30px;
line-height: 1;
font-family: FontAwesome;
text-align: center;
color: #f69c00;
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-uk-filter="dsgf" data-grid-prepared="true" style="position: absolute; box-sizing: border-box; padding-left: 10px; padding-bottom: 10px; top: 0px; left: 0px; opacity: 1;">
<div class="uk-panel">
<div class="uk-panel-teaser">
<figure class="uk-overlay uk-overlay-hover ">
<img src="/wp-content/uploads/bilder/projekte/dsf.jpg" class="uk-overlay-grayscale" alt="dfsg">
<div class="uk-overlay-panel uk-overlay-icon uk-overlay-fade"></div>
<a class="uk-position-cover" href="/wp-content/plugins/widgetkit/cache/nuding-35281426b204ba8667e05928e60e8a11.jpg" data-lightbox-type="image" data-uk-lightbox="{group:'.wk-1b2a'}" title="dsfg"></a>
</figure>
</div>
<div>
<div class="triggerinfo">
sdf
</div>
<div class="info">
<h5>dsfg</h5>
</div>
</div>
</div>
</div>
The Fiddlejsfiddle.net/e8qd8gvf/3/ works now as it should on my site. Now the thing is: on hover the img get colored and it appears a little icon in the bottom left coner, the trigger that is now under the img should be this little icon, because the icon is from the css definition in uk-overlay-icon (from the font awesome)
I dont now how to set the info class on this icon.
Or I was trying put an div with the info class over the img at the position of the icon and than trigger it, but than the colored effekt dont show when I trigger it, so I thought there must be a way to trigger the div on hover and not lose the colored effect, so the trigger div would trigger the Info and musst trigger the hover from the img at the same time
PS: Sorry for the long css !
The <figure> element is intended to mark up diagrams, illustrations, photos, code examples and similar content, "that can be moved away from the main flow of the document without affecting the document’s meaning" (http://w3c.github.io/html-reference/figure.html).
Your way of using it seems to be against this specification.
It's your own responsibility to code according to specification and best practices.
I just opted with your provided example: https://jsfiddle.net/e8qd8gvf/4/
I moved the uk-overlay-icon outside of the figure, added the toggle-info class and put the info box inside it.
All that was left was adding some CSS:
.uk-position-cover { cursor: default; }
.uk-panel-teaser { position: relative; }
.toggle-info {
display: none;
cursor: pointer;
position: absolute; bottom: 20px; left: 20px;
width: 30px; height: 30px;
}
.toggle-info > .info {
width: 150px; height: 150px;
border: 2px solid red;
position: absolute; bottom: -20px; left: 10px;
transform: translateY(100%);
}
.toggle-info, .info { display: inline-block !important; }
.toggle-info.hidden, .info.hidden { display: none !important; }
as well as changing your JS to:
jQuery(document).ready(function() {
jQuery(".uk-overlay").hover(
function() {
jQuery(this).next(".toggle-info").removeClass("hidden");
},
function() {
jQuery(this).next(".toggle-info").addClass("hidden");
}
);
jQuery(".toggle-info").hover(
function() {
jQuery(this)
.removeClass("hidden")
.children(".info").removeClass("hidden");
},
function() {
jQuery(this)
.addClass("hidden")
.children(".info").addClass("hidden");
}
);
});
 
My solution is only showing you a way to accomplish things and is by far not "nice". You need to adapt it yourself and to specifications.

Disable Anchor Within Hover Div on Mobile Until Open

I've searched high and low but can't find a solution to this exact problem.
On a desktop browser, when the user hovers over an image, a div appears and they can click the link within the div if they want. However, on a mobile device, the hover is triggered by a click. If the user clicks in just the right spot, even though the div isn't visible yet, they can accidentally click the anchor and navigate away from the page. (In other words, the div goes from display:none to display:block at the same time that the link is clicked.)
I want to prevent that accidental click from happening on mobile browsers, however I still want the link to be usable once the div is visible.
My code:
<style>
.staffpic {
position: relative;
width: 33.33333%;
height: auto;
}
.staffpic:hover .popup {
display: block;
}
.staffpic img {
display: block;
width: 110px;
height: 110px;
margin: 0 auto;
}
.popup {
display:none;
position: absolute;
bottom: 0;
left: -5px;
width: 100%;
height: 100%;
box-sizing: border-box;
padding: 15px;
background-color: rgba(255, 153, 0, 0.9);
color: #fff;
text-transform: uppercase;
}
</style>
<div class="staffpic">
<img src="/wp-content/uploads/image.jpg" />
<div class="popup">
John Smith, Director<br/>
CityName | Email John
</div>
</div>
Any ideas? HTML, CSS, JS and jQuery solutions are all welcome! (Maybe something more clever than what I can think of using pointer-events:none along with some jQuery?)
I'm actually about to encounter the same problem in a project, and jotted down a potential solution. Haven't tested it yet but it might help you out. The link should only trigger if the element has a display that's not 'none':
var popup = $('.popup'),
display = popup.css('display');
if (!(display === 'none')) {
popup.on('click', function(e) {
e.preventDefault();
});
}
I found a solution but it's not elegant. I wanted to post it in case someone has this problem in the future and just needs something that will work!
I added a fake link in a span with the real link then set new display styles for it and the real link based on the parent span is being hovered over.
<style>
.staffpic {
position: relative;
width: 33.33333%;
height: auto;
}
.staffpic:hover .popup {
display: block;
}
.staffpic img {
display: block;
width: 110px;
height: 110px;
margin: 0 auto;
}
.staffpic a {
display: none; /* Added */
}
.staffpic.link:hover a {
display: inline; /* Added */
}
.staffpic.link:hover .fakelink {
display: none; /* Added */
}
.popup {
display:none;
position: absolute;
bottom: 0;
left: -5px;
width: 100%;
height: 100%;
box-sizing: border-box;
padding: 15px;
background-color: rgba(255, 153, 0, 0.9);
color: #fff;
text-transform: uppercase;
}
</style>
<div class="staffpic">
<img src="/wp-content/uploads/image.jpg" />
<div class="popup">
John Smith, Director<br/>
CityName | <span class="link">Email John<span class="fakelink">Email John</span></span>
</div>
</div>
I'd still love a cleaner solution without all this added html if someone has it.

Categories