I am currently trying to replicate a website for practice, but have came to a halt due to a search box that I don't know how to create.
Now, I know how to create a normal search box, with the form and input method, however this specific search box has a slight animation.
I'll explain.
Search Box
Ok, so the search box only appears once you click the magnifying glass. Once you do this, the search box will sort of slide out from the left hand side of the magnifying glass.
How do I go about hiding the search box and only making it visible after being clicked on? And how do I make it "slide out" ?
My solution:
Use the following JavaScript code for appearing:
var button = document.getElementById("search");
var input = document.getElementById("search_input");
document.body.removeChild(input);
var permission = true;
button.addEventListener("click", appear);
function appear() {
if (permission == true) {
if(document.getElementById("search_input") == null || window.getComputedStyle(document.getElementById("search_input")).getPropertyValue("opacity") == 0) {
input.setAttribute("class", "static");
document.body.appendChild(input);
} else {
sliding();
}
}
}
function sliding() {
permission = false;
input.setAttribute("class", "sliding");
document.body.removeChild(input);
document.body.appendChild(input);
window.setTimeout(remove, 2900);
}
function remove() {
document.body.removeChild(input);
permission = true;
}
You only have to add "search" as id in the button tag or in the img tag and "search_input" as id in the input tag. And for sliding out you could use this CSS animation:
#keyframes slide {
from { left: 10%; opacity: 1;}
to { left: 90%; opacity: 0;}
}
#-webkit-keyframes slide {
from { left: 10%; opacity: 1;}
to { left: 90%; opacity: 0;}
}
#search_input {
position: absolute;
left: 10%;
}
.sliding {
animation-name: slide;
animation-duration: 3s;
-webkit-animation-name: slide;
-webkit-animation-duration: 3s;
}
Here's a simply jQuery solution.
$(document).ready(function() {
$('#trigger').click(function() {
$('#search-bar').toggleClass('search-bar-expanded');
});
});
#search-bar {
width: 0;
overflow: hidden;
display: inline-block;
transition:width 1s;
}
.search-bar-expanded {
width: 200px!important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div id="search">
<button><i class="fa fa-search" id="trigger"></i></button>
<div id="search-bar">
<input type="text"/>
</div>
</div>
Related
I have created an animation for an element on my page and it always runs when the page is refreshed but i would like the animation to play when an element is clicked. How would i go about doing this?
CSS:
#login-or-signup-selection {
display: flex;
animation-name: test;
animation-duration: 5s;
position: relative;
height: 70%;
}
#keyframes test {
0% {top: 0px;}
50% {top:300px}
100% {top: 0;}
}
HTML:
<p id="clickMe">Element to click</p>
$('#clickMe').click(function () {
$(this).addClass('login-or-signup-selection');
$(this).on("animationend", function(event) {
$(this).removeClass('login-or-signup-selection')
});
});
.login-or-signup-selection {
display: flex;
animation-name: test;
animation-duration: 5s;
position: relative;
height: 70%;
}
#keyframes test {
0% {top: 0px;}
50% {top:300px}
100% {top: 0;}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="clickMe">Element to click</p>
If you want only css and don't even care about js onclick events for now,
use the :active pseudo selector.
The only downside is that it only plays while (=during) e.g. the mouse button is down.
You would need some JavaScript for this.
First off, separate the CSS animation properties, and anything else related to your animation, and add them to their own class.
Next up, the JavaScript. You'll want to add an event listener to your element to add the animation class when clicked, and a timeout to remove the class afterwards so it will animate when clicked again.
const yourElement = document.getElementById('clickMe');
yourElement.addEventListener('click', _=> {
yourElement.classList.add('animation-class');
setTimeout(
_=> yourElement.classList.remove('animation-class'),
5000
)
});
#clickMe {
display: flex;
position: relative;
height: 70%;
}
.animation-class {
animation-name: test;
animation-duration: 5s;
}
#keyframes test {
0% { top: 0px; }
50% { top: 300px; }
100% { top: 0; }
}
<p id="clickMe">Element to click</p>
You need JavaScript for this.
If you add an event listener on the p element to listen for a click, this can then add the animation name to the selection div.
But you need to also listen for the end of the animation, otherwise subsequent clicks will have no effect. On animation end this snippet removes the animation name.
Note also that in order to be absolutely sure that the first (onload) animation end is trapped, the first animation name is not set until the event listeners have been set up.
function init() {
const clickMe = document.querySelector('#clickMe');
const selection = document.querySelector('#login-or-signup-selection');
clickMe.addEventListener('click', function() {
selection.style.animationName = 'test';
});
selection.addEventListener('animationend', function() {
selection.style.animationName = '';
});
selection.style.animationName = 'test';
}
window.onload = init;
#login-or-signup-selection {
display: flex;
animation-duration: 5s;
animation-iteration-count: 1;
position: relative;
height: 70%;
}
#keyframes test {
0% {
top: 0px;
}
50% {
top: 300px
}
100% {
top: 0;
}
}
<p id="clickMe">Element to click</p>
<div id="login-or-signup-selection">Login or signup selection</div>
I have a tab and once I click it the tab fades in. The content gets loaded in with AJAX. After the animation is done I want to load in the content. Right now the content is loading in immediately when I click the button. I tried toggleClass with delay but it didn't work.
How can I delay the content from being loaded in?
This is the HTML :
$("#button-1").on("click", function() {
$(".hidden-content-1", 2000).toggleClass("show-hidden-content", 2000);
$(".main-page-content-1", 2000).toggleClass("hide-shown-content", 2000);
})
#modal-1 {
width: 33.33%;
height: 100vh;
background-color: green;
left: 0;
top: 0;
}
.modals {
transition-timing-function: ease-out;
transition-duration: 1000ms;
position: absolute;
}
.active {
width: 100vw !important;
height: 100vh !important;
}
.show-hidden-content {
display: block !important;
}
.hidden-content-1 {
display: none;
}
.hide-shown-content {
display: none !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="modal-1" class="modals">
<div class="hidden-content-1">
<h1> TEST </h1>
</div>
<div class="main-page-content-1">
<h1>TEST </h1>
</div>
<a id="button-1" href="template-parts/panel1.php"><input onclick="change1()" type="button" value="See More" id="button-text-1"></input>
</a>
</div>
It seems you are looking something like:
$('#button-1').on('click', function () {
setTimeout(() => {
$('.hidden-content-1').toggleClass('show-hidden-content');
$('.main-page-content-1').toggleClass('hide-shown-content');
}, 2000);
});
You might want to use animation-delay
#target {
animation: fade-in 250ms ease-out 1s 1 normal both running;
}
#keyframes fade-in {
0% {
opacity:0;
} 100% {
opacity:1;
}
}
I am new to animations in CSS and this is my code
#keyframes fadeOut{
0% {opacity: 1;}
100% {opacity: 0;}
}
I want the above code to take effect on #menu only no matter where it is called, For EG: It is called on a button click but only affects the #menu element. Somewhat like and doesn't do anything to the button
button:focus{animation-name: fadeOut;
animation-duration: 4s;}
#keyframes fadeOut{
#menu{0% {opacity: 1;}}
#menu{100% {opacity: 0;}}
}
But the above code is illegal in CSS and hence, I can't use it.
Is there any alternate way to do this.
Thanks in advance.
You can do that by using JavaScript:
Example 1
In this example, we add an event for each element, each event calls the animation function that styles the #menu element.
let menu = document.getElementById('menu');
let btn = document.querySelector('.btn');
let p = document.querySelector('.p');
let box = document.querySelector('.box');
function animateIt(){
menu.style.animation = 'fade 1s';
setTimeout(function(){
menu.style.animation = 'unset';
}, 1000);
}
btn.addEventListener('click', (e) => {
animateIt();
});
p.addEventListener('click', (e) => {
animateIt();
});
box.addEventListener('click', (e) => {
animateIt();
});
#menu {
width: 200px;
height: 200px;
background-color: red;
margin-bottom: 20px;
opacity: 0;
}
.box {
background-color: blue;
color: #fafafa;
width: 200px;
}
#keyframes fade{
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<div id="menu"></div>
<button class="btn">Click</button>
<p class="p">Or Click Here</p>
<div class="box">Or Click Even Here</div>
Example 2
In this example, we simply add the elements that will have events that animates the #menu in an array of objects with the class name and the event listener for each one. Each element and it's event will basically call the animateIt() function that styles and animates the #menu.
All you have to do is to add a line for each element that will call the animation, including ONLY the class name, and the event that calls it.
// Get the #menu
let menu = document.getElementById('menu');
// Gather the elements in an array of objects including the class name and the event
let elements = [
{class: ".btn", evt: "click"},
{class: ".p", evt: "click"},
{class: ".box", evt: "mouseover"}
];
for(let i = 0; i < elements.length; i++){
// Get the element
let theTarget = document.querySelector(elements[i].class);
// Get the event
let targetEvt = elements[i].evt;
// Add event for each element to call the animation function
theTarget.addEventListener(targetEvt, (e) => {
animateIt();
});
}
// The animation function that styles (animate) the #menu
function animateIt(){
menu.style.animation = 'fade 1s';
setTimeout(function(){
menu.style.animation = 'unset';
}, 1000);
}
#menu {
width: 200px;
height: 200px;
background-color: red;
margin-bottom: 20px;
opacity: 0;
}
.box {
background-color: blue;
color: #fafafa;
width: 200px;
}
#keyframes fade{
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<div id="menu"></div>
<button class="btn">Click Here</button>
<p class="p">Or Click Here</p>
<div class="box">Or Hover Here</div>
As i understood , you have to add the animation to your element Like :
/* The animation code */
#keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
/* The element to apply the animation to */
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
}
In your case it will be :
#menu {
animation-name: fadeOut;
animation-duration: 4s;
}
You can change 4s to the duration you want.
I have two divs that appear like this:
The idea is that when you close the bottom div (click on the 'X'), it should disappear.
And when you close the top div, it should disappear, and also the bottom div should slide up and take its place.
I'm very new to jQuery, but this is my first attempt:
function initAnnouncements() {
$(document)
// Closes announcement modules
.on('click', 'annoucements-close', function () {
$('announcement-div').hide();
})
}
#keyframes slideInFromRight {
0% {
transform: translateX(100%);
}
.1%{
opacity: 1;
}
100% {
transform: translateX(0);
opacity: 1;
}
}
.announcements-container {
position: fixed;
top: 80px;
right: 20px;
z-index: 1001;
display: flex;
flex-direction: column;
width: 300px;
/* align-items: flex-end; */
}
.announcements-1 {
animation: slideInFromRight 0.4s ease;
opacity: 0;
animation-fill-mode: forwards;
}
.announcements-2 {
/* animation: 0.4s ease-out 0s 1 slideInFromRight; */
animation: slideInFromRight 0.4s ease;
opacity: 0;
animation-fill-mode: forwards;
animation-delay: .4s;
margin-top: 15px;
}
.annoucements-header {
background-color: #1481C3;
color: #ffffff;
font-family: "Proxima Nova Bold";
padding: 7px 10px;
}
.annoucements-close {
position: absolute;
right: 5px;
width: 24px;
height: 36px;
cursor: pointer;
opacity: .85;
}
.annoucements-close:hover {
opacity: 1;
}
.annoucements-close::before,
.annoucements-close::after {
content: '';
width: 24px;
height: 2px;
background: white;
position: absolute;
top: 7px;
left: 0;
}
.annoucements-close::before {
transform: rotate(-45deg);
}
.annoucements-close::after {
transform: rotate(45deg);
}
/*opened or closed*/
.announcement-div-opened {
display: none;
}
.announcement-div.opened .announcement-div-opened {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div class="announcements-container">
<div class="announcement-div announcements-1">
<div class="annoucements-header">
<span class="annoucement-type-quantity">2 School Announcements</span>
<i class="annoucements-close"></i>
</div>
</div>
<div class="announcement-div announcements-2">
<div class="annoucements-header">
<span class="annoucement-type-quantity">1 Admin Announcement</span>
<i class="annoucements-close"></i>
</div>
</div>
</div>
</body>
As you can see this isn't doing anything. I'm trying to toggle the class from 'open' (display:block) to 'closed' (display:none) when the annoucements-close <i> element is clicked on.
And ideally I would like for the second div to slide up when the top one is closed, but first I'd just like to get either one to disappear.
What's wrong with my code where that's not working as expected?
Link to JSFiddle
There are 2 issues with your code: the click() event is inside the function initAnnouncements that doesn't get called. You could move it outside of this function or call the function. Then you have issues with your selectors: It's
.on('click', '.annoucements-close', function () {
$('.announcement-div').hide();
})
instead of
.on('click', 'annoucements-close', function () {
$('announcement-div').hide();
})
for class selectors. Working Fiddle.
If you just want to hide the annoucement which was clicked upon, just change it to
.on('click', '.annoucements-close', function () {
$(this).closest('.announcement-div').hide();
})
I looked at your code and adjusted it a little to demonstrate:
Added your common class on the two announcements "announcement-div"
Attached the document click handler with the jQuery ready event
Used the delegated event selector to listen to clicks within the document that match that common selector
On click of one of the announcement-div's animate the height to 0 and then remove the element
Comments are included in the fiddle. Hope this is helpful!
// Fire this function when the document is ready
$(function() {
// Listen on the whole document for click events on the .announcement-div element
$(document).on('click', '.annoucements-close', function () {
// From the close button find the closest parent "announcement-div"
var annoucement = $(this).closest('.announcement-div');
// Function to run after animating the element (use .hide() to keep element but display:none)
function destroy() {
annoucement.remove();
}
// Animate the annoucement's height to 0 over 400ms and then call the destroy function
annoucement.animate({ height: "0px" }, 400, destroy);
});
});
Updated JS Fiddle
I have some CSS code that creates a typing animation (see snippet).
The two lines load simultaneously. How do I make them load one after another?
body{
background: #000;
color: lime;
}
div.a{
font-size: 14px;
margin: 30px;
white-space:nowrap;
overflow: hidden;
width: 30em;
animation: type 5s steps(50, end) 1;
}
#keyframes type{
from{ width: 0;}
}
#keyframes blink{
to{ opacity: .0;}
}
<div class="a">Supercalifragilisticexpialidocious<br /> Another sentence...</div>
Basically you can do that by checking whether the CSS3 Animation has ended or not like explained in this link
http://blog.teamtreehouse.com/using-jquery-to-detect-when-css3-animations-and-transitions-end
then create a function to apply the animation class and call it on jQuery ready, inside the function when the animation ended, check whether there's still another line of sentences that want to be animated
Here's the updated code that should work like what you wanted it to be
nb: this will work only if the sentences is only one line, and if it's more, you must separate it in another element like in the example, also the alert in the end is only to show that the function to animate the typing will not start anymore
nb2: I forgot that the question doesn't include the JavaScript or jQuery tag, but I hope this could help if by chance someone else needed to use the jQuery
var $typeAnimation;
$(function(){
$typeAnimation = $(".view").first();
if($typeAnimation.size() > 0) {
startAnimation();
}
});
function startAnimation() {
$typeAnimation.addClass("animate");
$typeAnimation.one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
function(e) {
$typeAnimation = $typeAnimation.next(".view");
if($typeAnimation.size() > 0) {
startAnimation();
} else {
alert("No More Sentence to be animated");
}
});
}
body{
background: #000;
color: lime;
}
.view {
display: none;
}
.view.animate{
display: block;
font-size: 14px;
margin: 30px;
white-space:nowrap;
overflow: hidden;
width: 30em;
animation: type 5s steps(50, end) 1;
}
#keyframes type{
from{ width: 0;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="view">Supercalifragilisticexpialidocious</div>
<div class="view">Another sentence...</div>
<div class="view">Yet Another sentences...</div>
<div class="view">And Also Another Final sentence...</div>