I have an image in a class called logo. The container .logo has an image background which I want to animate so it rotates 360 degrees indefinitely.
When I run the code it animates both images. I only want it to animate the css background property. Is this possible?
Here is my css so far.
a.navbar-brand {
background: url('http://i.imgur.com/3PL0u4Q.jpg');
background-repeat: no-repeat;
height: 180px;
width: 180px;
-webkit-animation: spin 16s linear infinite;
-moz-animation:spin 16s linear infinite;
animation:spin 16s linear infinite;
}
#-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
#-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
#keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
and the html
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img src="http://i.imgur.com/3PL0u4Q.jpg" alt="logo" class="logo"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>Default</li>
<li>Static top</li>
<li class="active">Fixed top <span class="sr-only">(current)</span></li>
</ul>
</div><!--/.nav-collapse -->
</div>
<div class="navbar-attatch"></div>
</nav>
Fiddle here
I know with css transitions there is an 'all' or 'css-property', but I am not sure how to insert with my example. I have tried multiple ways and it breaks the animation completely.
You can't rotate just the background. You can choose which properties animate, but you cannot say that only the background is going to have the transform applied to it.
In your case, you can rotate the image instead of the link. https://jsfiddle.net/mendesjuan/t8auvq39/1/
a.navbar-brand {
background: url('http://i.imgur.com/3PL0u4Q.jpg');
background-repeat: no-repeat;
height: 180px;
width: 180px;
}
a.navbar-brand img {
animation:spin 16s linear infinite;
}
#-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
#-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
#keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
<a class="navbar-brand" href="#"><img src="http://i.imgur.com/3PL0u4Q.jpg" alt="logo" class="logo"></a>
For future reference
Please notice how much code I've added to my answer and compare it to how much code you have in the question. Please make sure you remove unnecessary code in your questions
Related
I just migrate to Bootstrap V4. Im customizing the starter template: https://v4-alpha.getbootstrap.com/examples/starter-template/
I want the navbar to be transparent but change to dark when the viewport is sm and xs.
Im trying to change it using a breakpoint and adding a 'navbar-custom' class, but i doesn't change
html {
overflow: hidden;
height: 100%;
}
body {
padding-top: 5rem;
overflow: auto;
height: 100%;
background: linear-gradient(-45deg, #E8D1E2, #7CC4FF, #FFECBD, #BF9BFA);
background-size: 400% 400%;
-webkit-animation: Gradient 15s ease infinite;
-moz-animation: Gradient 15s ease infinite;
animation: Gradient 15s ease infinite;
}
#-webkit-keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
#-moz-keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
#keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
.navbar {
border-top: 2px solid #C66FF4;
width: 100%
}
#media (min-width: 768px) {
nav .navbar .navbar-custom {
background: rgba(0,0,0,0);
}
}
.starter-template {
padding: 3rem 1.5rem;
text-align: center;
}
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1 shrink-to-fit=no">
<meta name="description" content="Full-Stack Graphic Designer, Illustrator, Front-End Development">
<meta name="author" content="Liliana Orozco">
<meta name="keywords" content="Designer, Creativity, Design, CSS, HTML5, Branding, Logotype, Illustration, Web, Advertising">
<title>Randomood says: Welcome!</title>
<!-- Bootstrap 4.0 cdn -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<!--custom styles for this page -->
<link href="css/home.css" rel="stylesheet">
</head>
<body>
<!--navigation-->
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top navbar-custom">
<!--Brand in navigation menu-->
<a class="navbar-brand " href="#">Randomood</a>
<!--Brand image in navigation menu-->
<a class="navbar-brand" href="#"><img></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">Book</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="starter-template">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
</div><!-- /.container -->
<!--JS, Popper and JQuery-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="jquery-slim.min.js"><\/script>')</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
</body>
Easy with scss in your bootstrap theme overrides:
.bg-dark {
background-color: #343a40;
#include media-breakpoint-up(sm) {
background-color: transparent;
}
}
You are using Bootstrap's .bg-dark class in your <nav>. CSS declaration of this class is :
.bg-dark {
background-color: #343a40!important;
}
So, you are not allowed to override background-color property. In order to change the background-color property at particular screen break point, remove .bg-dark class using Javascript or Jquery and add your custom class which changes the background-color property of <nav>. I hope my explanation is clear to you.
I am attempting to animate the horizontal lines in the navbar to an 'X' when the page is reduced in size.
My navbar code is as follows:
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Company</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
About<b class="caret"></b>
<ul class="dropdown-menu">
<li>
About Us
</li>
<li>
Certification
</li>
<li>
Download PDF
</li>
</ul>
</li>
<li>
Products
</li>
<li>
Inquiry
</li>
<li>
Events
</li>
<li>
Contact
</li>
<li>
Login
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
When I attempt to use the CSS shown here it does not work. Is there something I need to modify for my navbar specifically? I also notice that when I add the css, the :
&:hover {
background: transparent !important;
}
the closing curly brace does not recognize the opening one. What am i missing?
It's because the tutorial is using Less, a CSS pre-processor, extending the CSS language.
However, you just have to use the compiled CSS, and make a few changes.
In your html, add some classes to the bars in order to distinguish each of them :
<span class="icon-bar top-bar"></span>
<span class="icon-bar middle-bar"></span>
<span class="icon-bar bottom-bar"></span>
Also, init your button with the class collapsed, or it will first render as a 'X'.
Then add the CSS compiled :
.navbar-toggle {
border: none;
background: transparent !important;
}
.navbar-toggle:hover {
background: transparent !important;
}
.navbar-toggle .icon-bar {
width: 22px;
transition: all 0.2s;
}
.navbar-toggle .top-bar {
transform: rotate(45deg);
transform-origin: 10% 10%;
}
.navbar-toggle .middle-bar {
opacity: 0;
}
.navbar-toggle .bottom-bar {
transform: rotate(-45deg);
transform-origin: 10% 90%;
}
.navbar-toggle.collapsed .top-bar {
transform: rotate(0);
}
.navbar-toggle.collapsed .middle-bar {
opacity: 1;
}
.navbar-toggle.collapsed .bottom-bar {
transform: rotate(0);
}
And now it should work like a charm.
Here's a JsFiddle : Demo
This worked for me
Theory
CSS provides all the necessary animation tools. Basically what's happening is this:
The top and bottom lines must rotate to form the X
The middle line must disappear
The X will be taller an more narrow than the hamburger lines, so:
The top and middle lines must move out vertically and to the right to maintain its center
Application
/* Define the shape and color of the hamburger lines */
.navbar-toggler span {
display: block;
background-color: #4f4f4f;
height: 3px;
width: 25px;
margin-top: 5px;
margin-bottom: 5px;
position: relative;
left: 0;
opacity: 1;
transition: all 0.35s ease-out;
transform-origin: center left;
}
/* top line needs a little padding */
.navbar-toggler span:nth-child(1) {
margin-top: 0.3em;
}
/**
* Animate collapse into X.
*/
/* top line rotates 45 degrees clockwise and moves up and in a bit to close the center of the X in the center of the button */
.navbar-toggler:not(.collapsed) span:nth-child(1) {
transform: translate(15%, -33%) rotate(45deg);
}
/* center line goes transparent */
.navbar-toggler:not(.collapsed) span:nth-child(2) {
opacity: 0;
}
/* bottom line rotates 45 degrees counter clockwise, in, and down a bit to close the center of the X in the center of the button */
.navbar-toggler:not(.collapsed) span:nth-child(3) {
transform: translate(15%, 33%) rotate(-45deg) ;
}
/**
* Animate collapse open into hamburger menu
*/
/* top line moves back to initial position and rotates back to 0 degrees */
.navbar-toggler span:nth-child(1) {
transform: translate(0%, 0%) rotate(0deg) ;
}
/* middle line goes back to regular color and opacity */
.navbar-toggler span:nth-child(2) {
opacity: 1;
}
/* bottom line goes back to initial position and rotates back to 0 degrees */
.navbar-toggler span:nth-child(3) {
transform: translate(0%, 0%) rotate(0deg) ;
}
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Bootstrap Navigation -->
<nav class="navbar bg-light">
<a class="navbar-toggler collapsed border-0" type="button" data-toggle="collapse" data-target="#collapsingNavbar">
<span> </span>
<span> </span>
<span> </span>
</a>
<a class="navbar-brand" href="./">
Brand
</a>
<div class="collapse navbar-collapse" id="collapsingNavbar">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
<main class="container">
<h1>Content Here</h1>
<p>Shrink the viewport if to expose the hamburger menu.</p>
</main>
What makes it work
Specifically, since the top and bottom lines rotate by 45 degrees to form the X, their center lines take up 70% of the width, so they must move in by 15%. This can be calculated using pythagorean theorem.
As it happens, our hamburger menu is 26x21 px, or 24% wider than it is tall, but the X ends up being 20x20 square when you move the lines into place and you take into account the height of the lines (here defined as 3px).
In this particular implementation, we are defining the point of rotation of each line as being the center-left. This affects how much we move the lines up, since the lines are about 3px tall, they each add about (2.1/2)=1.05px to the height of the X, or about 33% of the height of the X.
Therefore 33% is how much they must move out vertically out so the two lines meet at the center of the X and form a 20x20px square.
Customizing
The X will always make a square, so to find out how much to move them by, you just need to know the width and height of your <span> bars and the height of the resulting hamburger icon.
Plug those numbers into this equation:
Or in code:
const line_width = 26; // px
const line_height = 3; // px
const hamburger_height = 21; // px
const x_width = x_height = 0.8 * line_width;
const line_move_y_percent = 100 * (line_width - x_width) / (2 * line_height)
const line_move_right_percent = 100 * (x_height - hamburger_height) / (2 * line_height)
I managed to adopt Adonis' excellent answer (go and upvote) to work on one of Bootstrap 5's navbar examples with some notable changes:
With Bootstrap 5's existing navbar-toggler-icon span, there only needs to be two extra spans for the icon bars, not three
The button needs to have the collapsed class in order to avoid initialising as an X instead of the menu
Made the animation a bit faster
With those changes in mind, here is the reflected markup for an animated hamburger menu animation in Bootstrap 5:
<nav class="navbar navbar-expand-lg navbar-light bg-light collapsed" aria-label="Eighth navbar example">
<div class="container">
<a class="navbar-brand me-6 me-xl-7" href="#">Brand</a>
<button class="navbar-toggler collapsed" type="button" data-bs-toggle="collapse" data-bs-target="navbarsExample07" aria-controls="navbarsExample07" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
<span></span>
<span></span>
</button>
<div class="collapse navbar-collapse" id="navbar-home">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link" href="#">Find My Car</a>
</li>
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link" href="#">Frequently Asked Questions</a>
</li>
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link" href="#">Testimonials</a>
</li>
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</div>
</nav>
And the CSS:
.navbar-toggler span {
display: block;
background-color: #000;
height: 3px;
width: 25px;
margin-top: 5px;
margin-bottom: 5px;
position: relative;
left: 0;
opacity: 1;
transition: all 0.2s ease-out;
transform-origin: center left;
}
.navbar-toggler span:nth-child(1) {
margin-top: 0.3em;
}
.navbar-toggler:not(.collapsed) span:nth-child(1) {
transform: translate(15%, -33%) rotate(45deg);
}
.navbar-toggler:not(.collapsed) span:nth-child(2) {
opacity: 0;
}
.navbar-toggler:not(.collapsed) span:nth-child(3) {
transform: translate(15%, 33%) rotate(-45deg) ;
}
.navbar-toggler span:nth-child(1) {
transform: translate(0%, 0%) rotate(0deg) ;
}
.navbar-toggler span:nth-child(2) {
opacity: 1;
}
.navbar-toggler span:nth-child(3) {
transform: translate(0%, 0%) rotate(0deg) ;
}
.navbar-toggle .icon-bar {
width: 26px;
height: 2px;
transition: all 0.2s;
}
This should do it, if not just try to adjust the width value in the range of 20px-26px.
Based on the answers above, my adaptation for Bootstrap 5.1. Working with booth
collapse & offcanvas. I used jQuery, but is not required.
Button:
<button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasNavbar" aria-controls="offcanvasNavbar">
<span></span><span></span><span></span>
</button>
JavaScript:
let $btn = $('.navbar-toggler');
$($btn.data('bs-target')).on('show.bs.collapse hide.bs.collapse show.bs.offcanvas hide.bs.offcanvas', function(){
$btn.toggleClass('opened');
});
Css:
.navbar-toggler {
border: 0;
box-shadow: none;
}
.navbar-toggler:focus {
box-shadow: none;
}
.navbar-toggler span {
display: block;
background-color: #4f4f4f;
height: 3px;
width: 25px;
margin-top: 5px;
margin-bottom: 5px;
position: relative;
left: 0;
opacity: 1;
transition: all 0.2s ease-out;
transform-origin: left center;
}
.navbar-toggler span:nth-child(1) {
margin-top: 0.3em;
}
.navbar-toggler:not(.opened) span:nth-child(1){
transform: translate(0%, 0%) rotate(0deg) ;
}
.navbar-toggler:not(.opened) span:nth-child(2) {
opacity: 1;
}
.navbar-toggler:not(.opened) span:nth-child(3) {
transform: translate(0%, 0%) rotate(0deg) ;
}
.navbar-toggler span:nth-child(1) {
transform: translate(15%, -33%) rotate(45deg);
}
.navbar-toggler span:nth-child(2) {
opacity: 0;
}
.navbar-toggler span:nth-child(3) {
transform: translate(15%, 33%) rotate(-45deg) ;
}
I decided to make something that would spin repeatedly on a gradient background. I've got the "spin repeatedly" part right, but the gradient background isn't working.
HTML:
<img src="http://i.imgur.com/gQioYac.png">
<img src="http://i.imgur.com/gQioYac.png">
<img src="http://i.imgur.com/gQioYac.png">
<img src="http://i.imgur.com/gQioYac.png">
<img src="http://i.imgur.com/gQioYac.png">
<div id="css-gradient"></div>
CSS:
img {
animation-name: rotate;
animation-iteration-count: infinite;
animation-duration: 1s;
animation-direction: reverse;
animation-timing-function: ease-in;
animation-play-state: running;
float: left
}
#keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
#css-gradient {
background: linear-gradient(blue, white)
height: 100px;
width: 100px;
}
}
I've also posted it as a fiddle so you can see it in action:
https://jsfiddle.net/ywtfsju1/
You had some mistakes with your brackets, here the working fiddle: https://jsfiddle.net/ywtfsju1/1/
#css-gradient { was in #keyframes rotate {, not it's after it.
You should use code indent to avoid such mistakes 😉
I am using pseudo class along with keyframes for the sliding animation of the images but I am not able to render the animation on the browser. Please help me to debug this code. Thanks in advance :).
<html>
<style>
#fullimage > li:target {
animation: slideImage 50s linear;
-webkit-animation: slideImage 50s linear;
-moz-animation: slideImage 50s linear;
}
#keyframes slideImage {
from { left: -700px; }
to { left: 0px; }
}
#-webkit-keyframes slideImage {
from {left: -700px; }
to { left: 0px; }
}
#-moz-keyframes slideImage {
from {left: -700px; }
to { left: 0px; }
}
</style>
<body>
<div id="wrapper">
<ul id="fullimage">
<li id="a">
<img src ="a.jpg" />
</li>
<li id="b">
<img src ="images/b.jpg" />
</li>
<li id="c">
<img src ="c.jpg" />
</li>
</ul>
<ul id="thumbimage">
<li>
<a href="#a">
First one
</a>
</li>
<li>
<a href="#b">
Second one
</a>
</li>
<li>
<a href="#c">
Third one
</a>
</li>
</ul>
</div>
</body>
</html>
Your animation and keyframes code looks fine, but you need to set the position property to have left to work.
#fullimage > li:target {
position: relative;
}
jsfiddle
I have a div I've animated on hover. However when I am not hovering the div won't disappear
This is what the entire thing looks like in action: http://jsfiddle.net/Vbxtc/
This is the html:
<nav>
<div id="controls">
<button id="playButton">Play</button>
<div id="defaultBar">
<div id="progressBar"></div>
</div>
<button id="vol" onclick="level()">Vol</button>
<button id="mute">Mute</button>
<button id="full" onclick="toggleFullScreen()">Full</button>
</div>
<div id="playlist" class="animated fadeInRight">
<div>cats</div>
<div>cats</div>
<div>cats</div>
</div>
</nav>
This is the CSS i've made:
#playlist{
position:absolute;
display:block;
border:1px solid red;
height: 82%;
width: 25%;
top: 20px;
right: 0px;
z-index: 2;
float:right;
padding: 0px;
margin: 0px;
color:white;
background-color:#999999;
opacity: 0;
}
#playlist:hover {
opacity: 1;
}
This is the animation im trying
.animated:hover {
-webkit-animation-fill-mode: both;
-moz-animation-fill-mode: both;
-ms-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
-webkit-animation-duration: 1s;
-moz-animation-duration: 1s;
-ms-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
}
.fadeInRight {
-webkit-animation-name: fadeInRight;
-moz-animation-name: fadeInRight;
-o-animation-name: fadeInRight;
animation-name: fadeInRight;
}
#-webkit-keyframes fadeOutRight {
0% {
opacity: 1;
-webkit-transform: translateX(0);
}
100% {
opacity: 0;
-webkit-transform: translateX(20px);
}
}
#-webkit-keyframes fadeInRight {
0% {
opacity: 0;
-webkit-transform: translateX(20px);
}
100% {
opacity: 1;
-webkit-transform: translateX(0);
}
}
I noticed that when you time the mouse over exactly right (hover for about 1 second and move mouse up top), it DOES fade out nicely.
The other thing is, if you add the class fadeOutRight as follows:
<div id="playlist" class="animated fadeInRight fadeOutRight">
It fades out too quickly.
I know I didn't help much but the answer lies in the timing.
Also, if you had the fadeOutRight class on, for example, the sidebar, it works nicely!
<aside id="sidebar" class="fadeOutRight">
Perhaps, put the class of fadeOutRight on everything EXCEPT the fadeInRight div.
It's not a good idea to play with an element position in the hover state.
Even if you get to program it right (that is not easy), most of the time the user won't understand what's happening.
You can get flickering scenarios where, without the user moving the cursor, your div leaves the cursor position, canceling the hover, the div re-entering the cursor, the hover triggering , and so on.
I would recomend to trigger the hover on another div that covers the full area where the moving div will be.