123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <nav class="navbar">
	<details class="bad">
		<summary>
			Best Foods (BROKEN mousover experience) 👎
		</summary>
		<div class="dropdown-wrapper">
			<ul>
				<li><a href="#">Shephard's Pie</a></li>
				<li><a href="#">Empanadas</a></li>
				<li><a href="#">Pound Cakes</a></li>
				<li><a href="#">Tacos</a></li>
				<li><a href="#">ALL OTHER PIES</a></ul>
		</div>
	</details>
</nav>
<br><br>
<nav class="navbar">
	<details class="good">
		<summary>
			Best Foods - FIXED Mouseover experience 👍
		</summary>
		<div class="dropdown-wrapper">
			<ul>
				<li><a href="#">Shephard's Pie</a></li>
				<li><a href="#">Empanadas</a></li>
				<li><a href="#">Pound Cakes</a></li>
				<li><a href="#">Tacos</a></li>
				<li><a href="#">ALL OTHER PIES</a></ul>
		</div>
	</details>
</nav>
<br>
<p>
	The broken menu above opens just fine because there's a mouseover event triggered on the summary element. But in this scenario, that summary has a set height and doesn't expand to the height of its other content when in the "open" state. So clicking on a link in the broken menu isn't possible, except by fluke.
</p>
<p>
	On the "fixed" menu at the bottom, the height of the summary element is still hard-coded, but the gap between the summary element and the "dropdown-wrapper" div is covered by an invisible <code>.dropdown-wrapper::before</code> pseudo-element. This means that as the user moves the pointer down, it's always over a "child" of the orginal summary element, and so never triggers a "mouseleave" on that element.</p>

<p>
	There's one other pseudo-element in use here, to create the little triangle pointing up to the menu trigger.
</p>
            
          
!
            
              
body {
	min-height: 600px;
}
summary::-webkit-details-marker {
	display: none;
}

p, .navbar {
	max-width: 400px;
	margin: 20px auto;
}

summary {
	cursor: default;
	background-color: white;
	outline: 1px solid green;
	padding: 2px;
}


summary:focus {
	background-color: lightblue
}
.dropdown-wrapper {
	background-color: lightblue;
	position: relative;
	z-index: 10;
}

.dropdown-wrapper::before {
	position: absolute;
	content: "";
	background-color: inherit;
	height: 12px;
	width: 12px;
	top: -5px;
	left: 8px;
	transform: rotate(45deg);
}

details.bad,
details.good{
	/* Fixed height! This will mean both "bad" and "good" menues have broken mouseover experiences */
	height: 18px;
/* But sometimes designs call for stuff like this, or you are adding functionality to an exsiting structure and there's not time to rewrite all the surrounding code to accommodate the change*/
}

details.bad summary {
	outline: 1px solid red;
}
details.good {
	position: relative;
}

details.good::before {
	content:"";
	width: 100%;
	height: 40px;
	position: absolute;
	bottom:-48px;
}

details[open] .dropdown-wrapper {
	animation: fade-in 0.5s forwards;
}

details.summary-closing[open] .dropdown-wrapper {
	animation: fade-out 0.5s forwards;
}

@keyframes fade-in {
	0% {
		transform: translateY(-20px);
		opacity: 0;
	}
	100% {
		transform: translateY(0);
		opacity: 1;
	}
}

@keyframes fade-out {
	0% {
		opacity: 1;
	}
	100% {
		opacity: 0;
		transform: translateY(-20px);
	}
}

            
          
!
            
              let summaries = document.querySelectorAll("summary");

summaries.forEach(summary => {
	let details = summary.closest("details");
	console.log(summary, details)
	summary.addEventListener("click", function(event) {
		console.log(event)
		// first a guard clause: don't do anything
		// if we're already in the middle of closing the menu.
		if (details.classList.contains("summary-closing")) {
			return;
		}
		// but, if the menu is open ...
		if (details.open) {
			// prevent default to avoid immediate removal of "open" attribute
			event.preventDefault();
			// add a CSS class that contains the animating-out code
			details.classList.add("summary-closing");
			// when enough time has passed (in this case, 500 milliseconds),
			// remove both the "open" attribute, and the "summary-closing" CSS
			setTimeout(function() {
				details.removeAttribute("open");
				details.classList.remove("summary-closing");
			}, 500);
		}
	});
	// when user hovers over the summary element,
	// add the open attribute to the details element
	summary.addEventListener("mouseenter", event => {
		details.setAttribute("open", "open");
	});

	// when the user moves the mouse away from the details element,
	// perform the out-animation and delayed attribute-removal
	// just like in the click handler
	details.addEventListener("mouseleave", event => {
		event.target.classList.add("summary-closing");
		setTimeout(function() {
			event.target.removeAttribute("open");
			event.target.classList.remove("summary-closing");
		}, 500);
		event.target.setAttribute("open", "open");
	});
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console