jQuery flip plugin switch function fails on switch - javascript

I have a piece of code where I have two buttons and when I press one button I want a div to flip on the y-axis and when I press the other button I want the same div to flip on the x-axis. this works just fine when I use the button for the y-axis even if the previous flip was on the x-axis. But when I want to switch from the y-axis to the x-axis I need to press the x button twice because the first time nothing happens.
So desired behavior is:
click y button flip the div on the y-axis.
click x button flip the div on the x-axis.
current behavior is:
click y button div flips on the y-axis.
click x button nothing happens
click x button again div flips on the x-axis.
I have no idea what the problem is, this is the best i got so far.
All help is appreciated.
var ax = 'x';
$(document).ready(function() {
$('#card').flip({
trigger: 'manual',
axis: 'x'
});
$('#left').click(function() {
if (ax != 'y') {
$("#card").flip({
axis: 'y'
});
$("#card").on('flip:change', function() {
$('#card').flip('toggle');
});
ax = 'y';
} else {
$("#card").flip('toggle');
}
});
$('#right').click(function() {
if (ax != 'x') {
$("#card").flip({
axis: 'x'
});
$("#card").on('flip:change', function() {
$('#card').flip('toggle');
});
ax = 'x';
} else {
$("#card").flip('toggle');
}
});
});
#card {
position: fixed;
left: 50px;
top: 50px;
width: 200px;
height: 200px;
}
.front {
width: 100%;
height: 100%;
background-color: red;
}
.back {
width: 100%;
height: 100%;
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script>
<button id="left">y</button>
<button id="right">x</button>
<div id="card">
<div class="front">Front content</div>
<div class="back">Back content</div>
</div>

You over-complicated the problem. Here is a solution inspired by the documentation. All you have to do is to call flip to change the axis on click on he buttons, and to listen on flip:change event which is fired every time you call flip to change the orientation in order to actually flip it.
// Configure it to be manually flipped
$("#card").flip({
trigger: 'manual'
});
// Change the axis
$('#left').click(function() {
$("#card").flip({
axis: 'y'
});
});
$('#right').click(function() {
$("#card").flip({
axis: 'x'
});
});
// Toggle it on change
$("#card").on('flip:change', function() {
$('#card').flip('toggle');
});
#card {
position: fixed;
left: 50px;
top: 50px;
width: 200px;
height: 200px;
}
.front {
width: 100%;
height: 100%;
background-color: red;
}
.back {
width: 100%;
height: 100%;
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script>
<button id="left">y</button>
<button id="right">x</button>
<div id="card">
<div class="front">Front content</div>
<div class="back">Back content</div>
</div>

Here you go with a solution
var ax = 'x';
$(document).ready(function() {
$('#card').flip({
trigger: 'manual',
axis: 'x'
});
$("#card").on('flip:change', function() {
$('#card').flip('toggle');
});
$('#left').click(function() {
if (ax != 'y') {
$("#card").flip({
axis: 'y'
});
ax = 'y';
} else {
$("#card").flip('toggle');
}
});
$('#right').click(function() {
if (ax != 'x') {
$("#card").flip({
axis: 'x'
});
ax = 'x';
} else {
$("#card").flip('toggle');
}
});
});
#card {
position: fixed;
left: 50px;
top: 50px;
width: 200px;
height: 200px;
}
.front {
width: 100%;
height: 100%;
background-color: red;
}
.back {
width: 100%;
height: 100%;
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script>
<button id="left">y</button>
<button id="right">x</button>
<div id="card">
<div class="front">Front content</div>
<div class="back">Back content</div>
</div>
You should keep the flip:change outside the click event.
Hope this will help you.

You ended up binding the flip:change event multiple times with each click. That caused x to flip twice when clicked after Y was clicked twice. Hence it looks like nothing is happening.
You can see that happening when adding a debugger; at the start of $('#right').click(function() { and using the console to debug your code.
Anyway, adding the event binding ones only fixes it as that is all you need.
Removing all instance of $("#card").on('flip:change', function() { in your code and only adding it one time, fixes the problem.
In this example I added it at the bottom of your code.
var ax = 'y';
$(document).ready(function() {
$('#card').flip({
trigger: 'manual',
axis: 'y'
});
$('#left').click(function() {
//debugger;
if (ax != 'y') {
$("#card").flip({
axis: 'y'
});
ax = 'y';
} else {
$("#card").flip('toggle');
}
});
$('#right').click(function() {
//debugger;
if (ax != 'x') {
$("#card").flip({
axis: 'x'
});
ax = 'x';
} else {
$("#card").flip('toggle');
}
});
// Only bind ones.
$("#card").on('flip:change', function() {
$('#card').flip('toggle');
});
});
#card {
position: fixed;
left: 50px;
top: 50px;
width: 200px;
height: 200px;
}
.front {
width: 100%;
height: 100%;
background-color: red;
}
.back {
width: 100%;
height: 100%;
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script>
<button id="left">y</button>
<button id="right">x</button>
<div id="card">
<div class="front">Front content</div>
<div class="back">Back content</div>
</div>

Related

Javascript change color of button by hovering OUTSIDE it

I can hover on the button using jquery. Can someone please point me to the right direction on how to hover on the
function setColor(btn, color) {
var property = document.getElementById(btn);
if (window.getComputedStyle(property).BackgroundColor == 'rgb(0, 0, 0)') {
property.style.backgroundColor = color;
} else {
property.style.backgroundColor = "#000";
}
}
function setColor(btn, color) {
var property = document.getElementById(btn);
if (window.getComputedStyle(property).backgroundColor == 'rgb(244, 113, 33)') {
property.style.backgroundColor = color;
} else {
property.style.backgroundColor = "#f47121";
}
}
document.addEventListener('onclick', function(e) {
if (!(e.id === 'btnHousing')) {
document.getElementById('btnHousing').property.style.backgroundColor = '#FFF'
}
});
$(document).ready(function() {
$("button").hover(function() {
$(this).css("backgroundColor", "#fff");
}, function() {
$(this).css("backgroundColor", "#9d9d9d");
});
});
div {
height: 100px;
width: 300px;
background-color: black;
}
button {
height: 50px;
width: 150px;
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown" id="notification-dropdown" onclick="setColor('btnBell','#9d9d9d');">
<button type="button" id="btnBell">
</button>
</div>
area also and make the button white on hover?
Please check!
https://jsfiddle.net/obw6ec9v/
There is no need of javascript just css will do.
Also if you want to retain the color, on click you can use a additional class like
jQuery(function($) {
$('#notification-dropdown').click(function() {
$(this).find('button').addClass('clicked');
})
})
div {
height: 100px;
width: 300px;
background-color: black;
}
button {
height: 50px;
width: 150px;
position: relative;
background-color: #9d9d9d;
}
#notification-dropdown:hover button {
background-color: #fff;
}
#notification-dropdown button.clicked {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown" id="notification-dropdown" onclick="setColor('btnBell','#9d9d9d');">
<button type="button" id="btnBell"></button>
</div>
With javascript
$(document).ready(function() {
$("#notification-dropdown").hover(function(e) {
var $btn = $('button', this);
if (!$btn.hasClass('clicked')) {
$btn.css("backgroundColor", e.type == 'mouseenter' ? "#fff" : '#9d9d9d');
}
}).click(function() {
$('button', this).addClass('clicked').css("backgroundColor", 'red');
})
});
div {
height: 100px;
width: 300px;
background-color: black;
}
button {
height: 50px;
width: 150px;
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown" id="notification-dropdown">
<button type="button" id="btnBell"></button>
</div>
You can also replace onclick with onmouseover
<div class="dropdown" id="notification-dropdown" onmouseover="setColor('btnBell','#9d9d9d');">
See JSFiddle
You can just change the target you're listen for hover to that div, then use $("#btnBell") instead of $(this) to change the button's color.
function setColor(btn,color){
var property=document.getElementById(btn);
if (window.getComputedStyle(property).backgroundColor == 'rgb(244, 113, 33)') {
property.style.backgroundColor=color;
}
else {
property.style.backgroundColor = "#f47121";
}
}
document.addEventListener('onclick', function(e){
if(!(e.id === 'btnHousing')){
document.getElementById('btnHousing').property.style.backgroundColor = '#FFF'
}
});
$(document).ready(function(){
$("div").hover(function(){
$("#btnBell").css("backgroundColor", "#fff");
}, function(){
$("#btnBell").css("backgroundColor", "#9d9d9d");
});
});
div{
height:100px;
width:300px;
background-color:black;
}
button{
height:50px;
width:150px;
position:relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="dropdown" id="notification-dropdown" onclick="setColor('btnBell','#9d9d9d');">
<button type="button" id="btnBell" >
</button>
</div>

Toggling divs to slide at certain times

Please see this fiddle http://jsfiddle.net/rabelais/6bt70uhj/9/
$('#name-a').click(function() {
$('#bio-line-1').animate({width: 'toggle'});
window.setTimeout(function (){$('#bio-line-2').slideToggle( "slow" ); }, 300);
When clicking the link the first lines slides in from the left, and then when that is finished the second line slides in from the top. When it slides back to be hidden, the first line slides aways before the second line does.
How can I change it so the second line slides away before the first line slides away?
$('#name-a').click(function () {
if ($('#bio-line-1').css('display') == 'none') {
$('#bio-line-1').animate({ width: 'toggle' });
window.setTimeout(function () {
$('#bio-line-2').slideToggle("slow");
}, 300);
}
else {
$('#bio-line-2').slideToggle("slow");
window.setTimeout(function () {
$('#bio-line-1').animate({ width: 'toggle' });
}, 300);
}
});
DEMO
I think this is what you want:
$('#name-a').click(function () {
window.setTimeout(function () {
$('#bio-line-2').slideToggle("slow", function () {
$('#bio-line-1').animate({
width: 'toggle'
});
});
}, 300);
});
Demo: https://jsfiddle.net/tusharj/6bt70uhj/11/
I think you can do
var $l2 = $('#bio-line-2'),
$l1 = $('#bio-line-1');
var $a = $('#name-a').click(function() {
$a.toggleClass('visible');
var visible = $a.hasClass('visible'),
$f = visible ? $l1 : $l2,
$s = visible ? $l2 : $l1;
$f.animate({
width: 'toggle'
});
window.setTimeout(function() {
$s.slideToggle("slow");
}, 300);
});
#name-a {
left: 38px;
position: fixed;
top: 38px;
z-index: 1;
}
#bio-line-1 {
left: 150px;
position: fixed;
top: 35px;
width: 633px;
z-index: 1;
}
#bio-line-1 p {
color: #333333;
display: block;
float: right;
font-size: 16px;
line-height: 21px;
width: 552px;
}
#bio-line-2 {
left: 150px;
margin-top: 20px;
position: fixed;
top: 38px;
width: 633px;
z-index: 1;
}
#bio-line-2 p {
color: #333333;
display: block;
float: right;
font-size: 16px;
line-height: 21px;
width: 552px;
}
.hidden {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="name-a">
John Love
</div>
<div id="bio-line-1" class="hidden">
<p>holds a Master's Degree from the University of the Arts</p>
</div>
<div id="bio-line-2" class="hidden">
<p>London and currently works in London.</p>
</div>
$('#name-a').click(function() {
if($("#bio-line-2").is(":visible")){
window.setTimeout(function (){
$('#bio-line-1').animate({width: 'toggle'});
}, 300);
$('#bio-line-2').slideToggle( "slow" );
}
else{
window.setTimeout(function (){
$('#bio-line-2').slideToggle( "slow" );
}, 300);
$('#bio-line-1').animate({width: 'toggle'});
}
});
i have made some condition that while showing the lines, line number 1 will appear first and then line number 2 will appear. and while hiding lines, line number 2 will hide first and then line number 1 will hide... i think this is what you trying
Here is fiddle link
Thank You
I'd do something like this. I would also not use a setTimeout as it is not a reliable way to trigger consecutive animations. The timer can trigger at different times depending on what is happening in the UI thread and therefore ruining the effect you are trying to create. You should use the animate success callback of each animation in order to trigger the next one at the exact right time.
http://jsfiddle.net/6bt70uhj/23/
var $line1 = $('#bio-line-1'),
$line2 = $('#bio-line-2');
$('#name-a').click(function() {
if (!$line1.is(":visible")) {
$line1.animate({
width: 'toggle'
}, 300, function () {
$line2.slideToggle("slow");
});
} else {
$line2.slideToggle("slow", function() {
$line1.animate({
width: 'toggle'
});
});
}
});

Animating multiple divs by switching their classes

i am having a hard time trying to animate this box, so the changes go smooth, but i just cannot figure out how to keep everything together. Help would be really appreciated. (already tried with 'switchClass') Here is the whole code:
<script src="jquery.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<style>
#box {
position: relative;
margin: auto;
padding: auto;
display: block;
width: 167px;
height: 167px;
}
#box .item {
position: relative;
margin: 0;
display: block;
width: 100%;
height: 33%;
cursor: pointer;
}
#box .over {
height: 84%;
}
#box .other {
height: 8%;
}
#top {
background: red;
}
#mid {
background: green;
}
#bot {
background: blue;
}
</style>
<script>
function anim(item) {
$('.item').attr('class', 'item other');
$('#' + item.id).attr('class', 'item over');
}
function clean() {
$('.item').attr('class', 'item');
}
</script>
<div id='box' onmouseout="clean()">
<div id='top' class='item' onmouseover='anim(this)'></div>
<div id='mid' class='item' onmouseover='anim(this)'></div>
<div id='bot' class='item' onmouseover='anim(this)'></div>
</div>
edit: this code is running just fine, but its just an example of final output (just some animations needed)
Maybe this is not super cool, but seems to do job:
var $items = $('.item').on({
mouseover: function () {
$items.removeClass('over other');
$items.stop().filter(this).animate({height: '84%'}, function () {
$(this).addClass('over');
})
.end().not(this).animate({height: '8%'}, function () {
$(this).addClass('other');
});
},
reset: function() {
$items.removeClass('over other').stop().animate({height: '33%'});
}
});
$('#box').mouseout(function() {
$items.trigger('reset');
});
http://jsfiddle.net/dfsq/4vnkh/1/
If you want to animate the change, please take a look at jQuery animate
Something like this:
$('.item').mouseenter(function() {
$('.item').animate({
height: 80%
}, 500, function() {
// Animation complete.
});
});
$('.item').mouseleave(function() {
$('.item').animate({
height: 33%
}, 500, function() {
// Animation complete.
});
});
in this case you don't need onmouseout or onmouseover
If your animation is based solely on CSS class attributes why not use CSS3 hover pseudo-selector?
Example:
.box {
width: 200px;
}
.box:hover {
width: 400px;
}
<div class="box">Hover over me!</div>
Additional: Response to comments
If you are looking for custom animation duration you can use a callback function with a duration for the initial function call. Here's an example:
$('#div').animate({
width: '200px',
color: 'blue'
}, 5000, function() {
// Animation finished after 5 seconds.
alert("Animation complete!");
});
Addition #2
Your problem child is this little guy:
$('.item').attr('class', 'item other');
This sets each box to 8% height and THEN expands the primary animating box. Remove this and your #box will remain the same height throughout all animations!

How to Animate (slide) content adjacent to content with jquery toggle()

I am setting up a page which the user can hide the side bar if they wish. I am trying to use the jqeuryui to do this with the following js code
// TOGGLE JS
$(function () {
function runEffect() {
var options = {};
$("#effect").toggle('slide', options, 500);
};
$("#button").click(function () {
runEffect();
return false;
});
});
I have this working in a JSFiddle here http://jsfiddle.net/jwg4U/
However, when you look at the JSFiddle you will notice that my main content area which is a DIV called #content does not animate, it just jumps into place when I toggle the sidebar.
I would like the content div to also slide into place seamlessly and follow the toggle as it if it attached to it.
I have looked at jquery animate, but not sure how to implement this with the slide?
A Second part I am struggling with is how to change the button text when the sidebar is closed to say "Show Sidebar" - Weather it is open or closed right now it just says "Hide Sidebar"
Looking for some help
Thanks
See this updated fiddle : http://jsfiddle.net/jwg4U/23/
HTML:
<div id="container" style="width:800px">
<div id="header">
<h1>HEADER</h1>
</div>
<div class="toggler">
<div id="effect" class="ui-widget-content ui-corner-all">
<div id="menu" style="background-color:#FFD700;height:300px;width:100px;float:left;">
<h3>SIDEBAR</h3>
</div>
</div>
<div id="content" style="background-color:#EEEEEE;height:300px;">Main Content goes here</div>
</div>
Hide Sidebar
<div id="footer" style="background-color:#FFA500;clear:both;text-align:center;">
FOOTER</div>
</div>
​
JS:
// TOGGLE JS
$(function() {
var i = 0;
function runEffect() {
var options = {};
if (i === 0) {
i = 1;
$(".toggler").animate({
left: -100
}, {
duration: 500
});
}
else {
i = 0;
$(".toggler").animate({
left: 0
}, {
duration: 500
});
}
}
$("#button").click(function() {
if (i === 0) {
$(this).html("Show Sidebar");
}
else {
$(this).html("Hide Sidebar");
}
runEffect();
return false;
});
});
// TABS JS
$(function() {
$("#tabs").tabs();
});​
CSS:
.toggler {
float: left;
position: relative;
}
#button {
padding: .5em 1em;
text-decoration: none;
}
#effect {
position: relative;
float: left;
}
#content{
position: relative;
float: left;
width: 500px;
}
#button{
float: left;
clear: both;
}
#header{
background-color: #000;
color: #FFF;
}​
The jsfiddle for the complete answer : http://jsfiddle.net/jwg4U/22/
$('#effect').animate({
width: 'toggle',
height: 'toggle'
}, {
duration: 500,
specialEasing: {
width: 'linear',
height: 'linear'
},
complete: function() {
$("#content").animate(
{ left: '+=100px' },
60,
'easeInQuad',
function ()
{
if(isOpen)
{
isOpen=false;
$("#button").html('Show Sidebar');
}
else
{
isOpen=true;
$("#button").html('Hide Sidebar');
}
});
}
});

How to toggle (hide / show) sidebar div using jQuery

I have 2 <div>s with ids A and B. div A has a fixed width, which is taken as a sidebar.
The layout looks like diagram below:
The styling is like below:
html, body {
margin: 0;
padding: 0;
border: 0;
}
#A, #B {
position: absolute;
}
#A {
top: 0px;
width: 200px;
bottom: 0px;
}
#B {
top: 0px;
left: 200px;
right: 0;
bottom: 0px;
}
I have <a id="toggle">toggle</a> which acts as a toggle button. On the toggle button click, the sidebar may hide to the left and div B should stretch to fill the empty space. On second click, the sidebar may reappear to the previous position and div B should shrink back to the previous width.
How can I get this done using jQuery?
$('button').toggle(
function() {
$('#B').css('left', '0')
}, function() {
$('#B').css('left', '200px')
})
Check working example at http://jsfiddle.net/hThGb/1/
You can also see any animated version at http://jsfiddle.net/hThGb/2/
See this fiddle for a preview and check the documentation for jquerys toggle and animate methods.
$('#toggle').toggle(function(){
$('#A').animate({width:0});
$('#B').animate({left:0});
},function(){
$('#A').animate({width:200});
$('#B').animate({left:200});
});
Basically you animate on the properties that sets the layout.
A more advanced version:
$('#toggle').toggle(function(){
$('#A').stop(true).animate({width:0});
$('#B').stop(true).animate({left:0});
},function(){
$('#A').stop(true).animate({width:200});
$('#B').stop(true).animate({left:200});
})
This stops the previous animation, clears animation queue and begins the new animation.
You can visit w3school for the solution on this the link is here and there is another example also available that might surely help,
Take a look
The following will work with new versions of jQuery.
$(window).on('load', function(){
var toggle = false;
$('button').click(function() {
toggle = !toggle;
if(toggle){
$('#B').animate({left: 0});
}
else{
$('#B').animate({left: 200});
}
});
});
Using Javascript
var side = document.querySelector("#side");
var main = document.querySelector("#main");
var togg = document.querySelector("#toogle");
var width = window.innerWidth;
window.document.addEventListener("click", function() {
if (side.clientWidth == 0) {
// alert(side.clientWidth);
side.style.width = "200px";
main.style.marginLeft = "200px";
main.style.width = (width - 200) + "px";
togg.innerHTML = "Min";
} else {
// alert(side.clientWidth);
side.style.width = "0";
main.style.marginLeft = "0";
main.style.width = width + "px";
togg.innerHTML = "Max";
}
}, false);
button {
width: 100px;
position: relative;
display: block;
}
div {
position: absolute;
left: 0;
border: 3px solid #73AD21;
display: inline-block;
transition: 0.5s;
}
#side {
left: 0;
width: 0px;
background-color: red;
}
#main {
width: 100%;
background-color: white;
}
<button id="toogle">Max</button>
<div id="side">Sidebar</div>
<div id="main">Main</div>
$('#toggle').click(function() {
$('#B').toggleClass('extended-panel');
$('#A').toggle(/** specify a time here for an animation */);
});
and in the CSS:
.extended-panel {
left: 0px !important;
}
$(document).ready(function () {
$(".trigger").click(function () {
$("#sidebar").toggle("fast");
$("#sidebar").toggleClass("active");
return false;
});
});
<div>
<a class="trigger" href="#">
<img id="icon-menu" alt='menu' height='50' src="Images/Push Pin.png" width='50' />
</a>
</div>
<div id="sidebar">
</div>
Instead #sidebar give the id of ur div.
This help to hide and show the sidebar, and the content take place of the empty space left by the sidebar.
<div id="A">Sidebar</div>
<div id="B"><button>toggle</button>
Content here: Bla, bla, bla
</div>
//Toggle Hide/Show sidebar slowy
$(document).ready(function(){
$('#B').click(function(e) {
e.preventDefault();
$('#A').toggle('slow');
$('#B').toggleClass('extended-panel');
});
});
html, body {
margin: 0;
padding: 0;
border: 0;
}
#A, #B {
position: absolute;
}
#A {
top: 0px;
width: 200px;
bottom: 0px;
background:orange;
}
#B {
top: 0px;
left: 200px;
right: 0;
bottom: 0px;
background:green;
}
/* makes the content take place of the SIDEBAR
which is empty when is hided */
.extended-panel {
left: 0px !important;
}

Categories

Resources