I want to expand and decrease height of header when I press click on div "arrow". I've tried to addClass with Jquery, but it doesn't really work
HTML:
<header>
<p>The title..</p>
<h1>Some text</h1>
<div class="arrow" id="arrow">
<img src="img/arrow.svg" />
</div>
</header>
CSS
header {
background: #2282A4;
color: #fff;
text-align: center;
width: 100%;
height: 450px;
transition: height 0.5 ease;
}
expandheight {
height: 850px;
}
JQuery:
$(document).ready(function() {
$(function() {
$('#arrow').click(function() {
$('header').addClass('expandheight');
});
});
});
I don't know how I can decrease height now with the same button, to remove "expandheight" class when it is active and add it when it is not... I've tried if/else, I failed.
You have multiple syntax errors:
expandheight should be styled using .expandheight
use cross browser animation properties
use toggleClass to add/remove class
$(document).ready(function() {
$(function() {
$('#arrow').click(function() {
$('header').toggleClass('expandheight');
});
});
});
header {
background: #2282A4;
color: #fff;
text-align: center;
width: 100%;
height: 450px;
-webkit-transition:height 0.5s ease;
-moz-transition:height 0.5s ease;
transition:height 0.5s ease;
}
.expandheight {
height: 850px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<header>
<p>The title..</p>
<h1>Some text</h1>
<div class="arrow" id="arrow">
<img src="img/arrow.svg" />
</div>
</header>
If you want to expand and decrease the height of the header, use toggleClass() rather than using addClass()
jQuery(document).ready(function() {
jQuery(function() {
jQuery('#arrow').click(function() {
jQuery('header').toggleClass('expandheight');
});
});
});
Also, you had a few errors in your code. I have created a jsfiddle to show you it working.
https://jsfiddle.net/7yodz723/
(I put a border around the arrow just so we can clearly see the example working)
Just use toggle class to switch classes.
$(document).ready(function() {
$(function() {
$('#arrow').click(function() {
$('header').toggleClass('expandheight');
});
});
});
header {
background: #2282A4;
color: #fff;
text-align: center;
width: 100%;
height: 450px;
transition: height 0.5 ease;
}
.expandheight {
height: 850px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<header>
<p>The title..</p>
<h1>Some text</h1>
<div class="arrow" id="arrow">
<img src="img/arrow.svg" />
</div>
</header>
Related
I am trying to create an animated sidebar. Initially i have a top navbar, a sidebar and a content div. Sidebar and content divs are inside a container div which has display flex row property. I am confused about transition. I want the sidebar show and hide when nav home is clicked which has a sliding effect. Right now I have a sliding sidebar but the content div is not getting full width of its parent when sidebar hides. How can I do this? Should I apply transition on flex or on transform? Any help is appreciated.
<body>
<nav>
<ul>
<li><button id="home">Home</button></li>
<li><button>Services</button></li>
<li><button>Operations</button></li>
<li><button>About</button></li>
<li><button>Contact</button></li>
</ul>
</nav>
<div class="container">
<div id="sidebar" class="sidebar visible">
<ul>
<li><button>Inventories</button></li>
<li><button>Employees</button></li>
<li><button>Feedback</button></li>
<li><button>Projects</button></li>
</ul>
</div>
<div class="content" id="content">
LOrem issum dolor sit amet
</div>
</div>
</body>
nav{
width: 100%;
display: flex;
justify-content : center;
}
.container{
display: flex;
flex-direction: row;
}
.sidebar{
flex:1;
background-color: rgb(40,100,250);
height: 100vh;
transition: transform 0.5s linear;
transform: translatex(-100%);
}
.visible{
transform: translatex(0);
flex: 1;
}
.content{
background-color: rgb(33,31,31);
flex: 5;
text-align: center;
color: #fff;
transition: flex 0.5s ease-in-out;
}
.afterContent{
flex: 10;
}
const homeButton = document.getElementById("home");
const sidebarDiv = document.getElementById("sidebar");
const content = document.getElementById("content");
homeButton.addEventListener("click", () => {
if (sidebarDiv.classList.contains("visible")) {
sidebarDiv.classList.remove("visible");
if(!content.classList.contains("afterContent")){
content.classList.add("afterContent");
}
} else {
sidebarDiv.classList.add("visible");
if(content.classList.contains("afterContent")){
content.classList.remove("afterContent");
}
}
});
Problem is that you just transform element, transforming will not change layout structure, it only affects element that you apply transform to and all its children.
Usually to achieve the result you want padding and position: absolute.
You have your sidebar element, that is position: absolute; top: 0; bottom: 0; left: 0;. To hide it you just transform it to the left. And then you have your content element that offsets menu with padding-left.
Here is a simple demo:
<html>
<head>
<style>
html, body {margin: 0; padding: 0}
.header {
width: 100%;
height: 100px;
background-color: red;
}
.body {
position: relative;
}
.sidebar {
position: absolute;
background-color: blue;
top: 0;
bottom: 0;
left: 0;
width: 300px;
transition: transform 0.2s;
}
.content {
padding-left: 300px;
min-height: 500px;
background-color: teal;
transition: padding-left 0.2s;
}
.menu-closed > .sidebar {
transform: translateX(-300px);
}
.menu-closed > .content {
padding-left: 0px;
}
</style>
</head>
<body>
<div class="header">header menu</div>
<div id="body" class="body">
<nav class="sidebar">sidebar menu</nav>
<div class="content"><button onclick="menu()">nav</button> content</div>
<div>
<script>
const body = document.getElementById("body");
let menuClosed = false;
function menu() {
menuClosed ? body.classList.remove("menu-closed") : body.classList.add("menu-closed");
menuClosed = !menuClosed;
}
</script>
</body>
</html>
I want to expand a div to full screen on clicking on it. Just like
this Fiddle js link here
I want to animate the same from its position. if I click the box it feels like expanding from its position please help me to achieve that
$('.myDiv').click(function(e){
$(this).toggleClass('fullscreen');
});
.myDiv{background:#cc0000; width:100px; height:100px;float:left:margin:15px;}
.myDiv.fullscreen{
z-index: 9999;
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myDiv">
my div
<button>Full Screen</button>
</div>
<div class="myDiv">
my div 2
<button>Full Screen</button>
</div>
Fullscreen animation
Now making a element fullscreen is pretty simple. It could be done with css alone.
.content {
display: inline-grid;
width: 150px;
height: 100px;
background-color: cornflowerblue;
border-radius: 3px;
transition: width 2s, height 2s;
margin: 10px;
}
.content button {
display: inline-block;
justify-self: center;
align-self: center;
height: 2em;
}
.content:hover {
width: 100vw;
height: 1200vh;
}
<div class="container">
<div class="content">
<button>Fullscreen</button>
</div>
</div>
<div class="content"></div>
Just adding a transition will make the element brake the layout.
To not break a layout you need:
Replace the element. (Below the Visibility : hidden element).
Give it an Absolute position.
Then set its width to fullscreen and animate its position so it can cover it.
Added an animation, transition
//Function is run on page load
$(function() {
var full = $(".fullscreen");
//Loops over all elements that have the class fullscreen
full.each(function(index, elem) {
$(elem).click(fullscreenClick);
});
function fullscreenClick() {
//The button is this
//We want to clone the parent
var box = $(this).parent();
//create a holder box so the layout stays the same
var holder = $(box).clone(false, true);
//and make it not visible
$(holder).css({
"visibility": "hidden"
});
//Get its position
var pos = $(box).position();
//Substitute our box with our holder
$(box).before($(holder));
//Set the position of our box (not holder)
//Give it absolute position (eg. outside our set structure)
$(box).css({
"position": "absolute",
"left": pos.left + "px",
"top": pos.top + "px",
});
//Set class so it can be animated
$(box).addClass("fullscreen");
//Animate the position
$(box).animate({
"top": 0,
"left": 0,
}, 3000);
}
});
* {
margin: 0;
padding: 0;
}
.container {
display: inline-block;
}
.container .element {
display: inline-block;
background-color: cornflowerblue;
margin: 5px;
padding: 10px;
width: 70px;
height: 30px;
transition: width 3s, height 3s;
;
}
.container .element.fullscreen {
width: calc(100vw - 30px);
height: calc(100vh - 30px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="element">
<button class="fullscreen">Fullscreen</button>
</div>
<div class="element">
<button class="fullscreen">Fullscreen</button>
</div>
<div class="element">
<button class="fullscreen">Fullscreen</button>
</div>
<div class="element">
<button class="fullscreen">Fullscreen</button>
</div>
<div class="element">
<button class="fullscreen">Fullscreen</button>
</div>
<div class="element">
<button class="fullscreen">Fullscreen</button>
</div>
<div class="element">
<button class="fullscreen">Fullscreen</button>
</div>
<div class="element">
<button class="fullscreen">Fullscreen</button>
</div>
</div>
You can add animation on all styles changes adding next properties to myDiv class:
/* Animate all changes */
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
I will show the changes on your example:
$('.myDiv').click(function(e)
{
$(this).toggleClass('fullscreen');
});
.myDiv{
background:#cc0000;
width:100px;
height:100px;
float:left:
margin:15px;
/*Animations*/
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
.myDiv.fullscreen{
z-index: 9999;
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myDiv">
my div 1
<button>Full Screen</button>
</div>
<div class="myDiv">
my div 2
<button>Full Screen</button>
</div>
It's a bit of a complicated task, but this should give you an idea of how it's done. This code will run into some issues (clicking quickly will stack setTimeout) but does the basics.
The idea is that you calculate the current position of the element with getBoundingClientRect() and set initial position values with that, so that when you adjust the position to fixed, it will look as though it's still in the same spot - then when you override those values with the .fullscreen css, the transition property will allow them to animate.
The biggest issue here, which you'll notice if you click on the first div, is that it disappears from the layout and causes the second div to jump up to where it was, you'd probably need a way of preserving the layout. Hopefully this is helpful as a starting point anyway.
function getPosition(elem){
const rect = elem.getBoundingClientRect()
return {
top: rect.top,
left: rect.left,
width: rect.width,
height: rect.height
}
}
function toPx(val){
return [val, 'px'].join('')
}
$('.myDiv').click(function(e){
if(this.classList.contains('fullscreen')){
this.classList.remove('fullscreen')
setTimeout(e => this.style.position = 'static', 1000)
//close
} else {
//open
let pos = getPosition(this)
this.style.width = toPx(pos.width)
this.style.height = toPx(pos.height)
this.style.top = toPx(pos.top)
this.style.left = toPx(pos.left)
console.log(pos)
this.classList.add('fullscreen')
this.style.position = 'fixed'
}
});
.myDiv{background:#cc0000; width:100px; height:100px;float:left:margin:15px;}
.myDiv.fullscreen{
z-index: 9999;
width: 100% !important;
height: 100% !important;
top: 0 !important;
left: 0 !important;
}
.animateTransitions {
transition: all 1s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myDiv animateTransitions">
my div
<button>Full Screen</button>
</div>
<div class="myDiv animateTransitions">
my div 2
<button>Full Screen</button>
</div>
Here's how i made it:
JQuery: 3.6.0
Css preprocessor: SCSS
CSS Framework: Bootstrap 5.1.0
See it in action:
https://codepen.io/illegalmexican/pen/NWYNVvg
Please note that i was answering a JQuery post. The fowlloing could be made in Vanilla Javascript for better performance. Also keep in mind that this could also be improved in terms of Accessibility standars by having a close button.
Html:
<div class="myDiv-Container">
<div class="row">
<div class="col-6">
<div class="d-flex flex-wrap position-relative">
<div class="myDiv w-50">
<div class="front bg-primary top-left">
<h1>Hello<br>World</h1>
</div>
</div>
<div class="myDiv w-50">
<div class="front bg-success top-right">
<h1>Hello<br>World</h1>
</div>
</div>
<div class="myDiv w-50">
<div class="front bg-danger bottom-left">
<h1>Hello<br>World</h1>
</div>
</div>
<div class="myDiv w-50">
<div class="front bg-warning bottom-right">
<h1>Hello<br>World</h1>
</div>
</div>
</div>
</div>
</div>
</div>
SCSS:
.myDiv-Container {
height: 500px;
width: 500px;
position: relative;
}
.front {
position: relative;
text-align: center;
display: flex;
&.position-absolute {
z-index: 1;
}
&.top-left {
top: 0;
left: 0;
}
&.bottom-left {
bottom: 0;
left: 0;
}
&.top-right {
top: 0;
right: 0;
}
&.bottom-right {
bottom: 0;
right: 0;
}
}
JQuery:
document.addEventListener('DOMContentLoaded', function () {
jQuery('.myDiv').each(function () {
var frontElem = jQuery(this).find('.front');
jQuery(this).on("click", function () {
if (jQuery(this).hasClass('active')) {
jQuery(this).removeClass('active');
jQuery(frontElem).animate(
{
width: jQuery(this).width(),
height: jQuery(this).height(),
},
500, function() {
jQuery(frontElem).removeClass('position-absolute');
jQuery(frontElem).css({
width: '100%',
height: '100%',
});
});
} else {
jQuery(frontElem).css({
width: jQuery(this).width(),
height: jQuery(this).height(),
});
jQuery(frontElem).addClass('position-absolute');
jQuery(this).addClass('active');
jQuery(frontElem).animate({
'width': '100%',
'height': '100%',
}, 500);
}
});
});
});
The menu works ok, but have no animation. I want the .fullpagenav on click comes out from the left to right, and when re-clicked returns back (from right to left). It was great if you can do it with toggle and animation, only for the short code.
I found this code on the following topic: Slide to side with jquery on click and toggle
So, I have this following code:
$( ".secondary-toggle" ).click(function() {
$('.fullpagenav').animate({width:'toggle'},350);
});
.fullpagenav {
position:fixed;
height: 100%;
width:100%;
z-index: 2;
background-color:#464646;
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fullpagenav">
<div id="secondary" class="secondary toggled-on" aria-expanded="true">
<nav id="site-navigation" class="main-navigation" role="navigation">
...
</nav>
</div>
</div>
Code above doesn't working, I create similiar code for you, to show You how could it be done. Its simply code, if you dont understand something, just ask me.
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script>
$(document).on('click', '.menu-item', function(e) {
var self = $(e.currentTarget);
self.toggleClass('menu-item-toggle')
});
</script>
<style>
.menu-item {
width: 50px;
height: 50px;
background-color: green;
position: relative;
left: 0;
transition: left 2s;
}
.menu-item-toggle {
left: 100px;
}
</style>
<div class="menu-item">Menu item</div>
https://jsfiddle.net/s7076ja4/
First you need to set it at the left most of the page and then create a class having position left:0 and then toggle that class on click and for the effects use transition.
$( ".secondary-toggle" ).click(function() {
if($(this).hasClass('active_nav')){
$('.fullpagenav').removeClass('active_nav');
}
else{
$('.fullpagenav').addClass('active_nav');
}
});
.fullpagenav {
position:fixed;
height: 100%;
width:100%;
z-index: 2;
background-color:#464646;
left:-100%;
transition: all 1s linear 1s;
-webkit-transition: all 1s linear 1s;
-moz-transition: all 1s linear 1s;
}
.active_nav{
left:0!important;
transition: all 1s linear 1s;
-webkit-transition: all 1s linear 1s;
-moz-transition: all 1s linear 1s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fullpagenav">
<div id="secondary" class="secondary toggled-on" aria-expanded="true">
<nav id="site-navigation" class="main-navigation" role="navigation">
...
</nav>
</div>
</div>
I placed the fullpagenav div to left most and on click on that div animates as you requested.
Below is the updated code
$( ".fullpagenav" ).click(function() {
$('.secondary').animate({width:'toggle'},350);
});
.fullpagenav {
position: fixed;
height: 100%;
width: 1%;
margin-left: -1%;
}
.secondary {
position: fixed;
height: 100%;
width: 10%;
background-color: #464646;
display: none;
padding-left: 1%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fullpagenav">
<div id="secondary" class="secondary toggled-on" aria-expanded="true">
<nav id="site-navigation" class="main-navigation" role="navigation">
...
</nav>
</div>
</div>
I am using JQuery slideUp/slideDown to add an overlay to an image that is hidden until mouseover and then slides up from the bottom of the image.
The div that is sliding up is given a background color, but it doesn't show up over the image, it shows up behind it (the text shows up on top of the image, but I can just see the bottom edge of the div's background color because of the margins I set). Z-index is already set to 100.
Any ideas? Thanks!
(function($) {
$(document).ready(function() {
$(".slider").attr("style", "display: none;");
if ($(".slider")) {
$('.image').mouseover(function() {
$(this).find(".slider").slideDown("400");
}).mouseout(function() {
$(this).find(".slider").slideUp("400");
});
}
});
}(jQuery));
.image{
height: 300px;
width: 300px;
background: #000000;
}
.slider {
background-color: #333333 !important;
background: #333333 !important;
background-position: 0% 100%;
color: #ffffff;
margin: -90px 0 0 0;
height: 90px;
width: 100%;
z-index: 100 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="image">
</div>
<div class="slider">
<div class="title">Title</div>
<div class="text">Text</div>
</div>
</div>
It's better to define display: none; for slider in css rule.
Also to bind mouseenter and mouseleave to container will prevent jumping slider when mouse is leaving image and moving over slider.
$('.container').on("mouseenter", function() {
$(".slider").slideDown();
});
$('.container').on("mouseleave", function() {
$(".slider").slideUp("400");
});
.container {
width: 300px;
}
.image{
height: 300px;
width: 300px;
background: #000000;
}
.slider {
background-color: #333333;
color: #ffffff;
margin: -90px 0 0 0;
height: 90px;
width: 100%;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="image">
</div>
<div class="slider">
<div class="title">Title</div>
<div class="text">Text</div>
</div>
</div>
z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).
If you do not define position, element will be static, as that's default position for block elements.
M.
I've created a gallery using just CSS that when a thumbnail is hovered on, the title slides into view. It sort of works on touch screens that if the button is touched and held, the title appears. I would like to make it so that a tap brings up the title and a second tap enters the gallery. I've tried all sorts of jQuery code but nothing seems to enable the second tap. I'd also wouldn't mind if it was one tap with a second or two delay to read the title before entering the gallery. I'm new to javascript and this site so I apologize if I don't ask this properly. Thanks for your help!
Here's my code:
<head>
<script src="js/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$("target").click(function(){ $(this).toggleClass("titleBox"); });
</script>
</head>
<body ontouchstart="" class="no-touch">
<div class="wrap">
<!-- Define all of the tiles: -->
<div class="box">
<div class="boxInner">
<a href="buildings/colorclock.html">
<img src="images/buildings/thumbs/06.jpg" alt="Color Clock House">
<div class="titleBox">Color Clock House</div>
</a>
</div>
</div>
<div class="box">
<div class="boxInner">
<a href="buildings/treetriangle.html">
<img src="images/buildings/thumbs/07.jpg" alt="Tree Triangle House">
<div class="titleBox">Tree Triangle House</div>
</a>
</div>
</div>
</div>
</body>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
.wrap {
overflow: hidden;
margin: auto;
padding: 0 10%;
margin-top: 40px;
}
.box {
float: left;
position: relative;
width: 20%;
padding-bottom: 20%;
}
.boxInner {
position: absolute;
left: 20px;
right: 20px;
top: 20px;
bottom: 20px;
overflow: hidden;
}
.boxInner img {
width: 100%;
}
.boxInner .titleBox {
position: absolute;
bottom: 0;
left: 0;
right: 0;
margin-bottom: -50px;
font-size: .9em;
background: #fff;
background-size: 105%;
color: #A59E97;
padding: 5px;
text-align: center;
-webkit-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
body.no-touch .boxInner:hover .titleBox, body.touch .boxInner.touchFocus .titleBox {
margin-bottom: 0;
}
.boxInner:focus {
cursor: pointer;
}
I was finally able to solve the click once, activate hover, click twice to to open the link on touch screens. If anyone is interested in the code here it is.
jQuery(function($) {
$('.boxInner').on("touchstart", function (e) {
'use strict'; //satisfy code inspectors
var link = $(this); //preselect the link
if (link.hasClass('hover')) {
return true;
} else {
link.addClass('hover');
$('a.taphover').not(this).removeClass('hover');
e.preventDefault();
return false;
}
});