Need some help on this please, I need to have the div close when a tab is open alternately, or one tab can be open and others will close automatically
Here is My fiddle I just got from someone
HTML:
<div class="siteMap">
<div class="mapButton"></div>
<div class="theMap" style="background:red;">content here</div>
</div>
<div class="siteMap" style="margin-top:70px;">
<div class="mapButton"></div>
<div class="theMap" style="background:blue;">content here</div>
</div>
JAVASCRIPT:
$('.mapButton').click(function() {
var mapPos = parseInt($(this).parent('.siteMap').css('left'), 10);
if (mapPos < 0) {
$(this).parent('.siteMap').animate({
left: '+=200'
}, 458, 'swing', function() {
// Animation complete.
});
}
else {
$(this).parent('.siteMap').animate({
left: '-=200'
}, 458, 'swing', function() {
// Animation complete.
});
}
});
CSS:
.siteMap {
width:200px;
position:fixed;
left:-200px;
top:0px;
display:block;
color:#FFF;
z-index:2;
opacity: 0.95;
}
.siteMap .mapButton {
display:block;
color:#333;
background-color:#ACACAC;
height:50px;
width:50px;
text-align:center;
position:absolute;
left: 200px;
cursor:pointer;
}
.siteMap .theMap {
width:100%;
height:100px;
background-color:#666;
position:relative;
}
Here is the fiddle
Try
$('.mapButton').click(function () {
var $siteMap = $(this).parent('.siteMap');
var mapPos = parseInt($siteMap.css('left'), 10);
if (mapPos < 0) {
$siteMap.animate({
left: '+=200'
}, 458, 'swing', function () {
// Animation complete.
});
} else {
$siteMap.animate({
left: '-=200'
}, 458, 'swing', function () {
// Animation complete.
});
}
$('.siteMap').not($siteMap).filter(function () {
return parseInt($(this).css('left'), 10) == 0
}).animate({
left: '-=200'
}, 458, 'swing', function () {
// Animation complete.
});
});
Demo: Fiddle
Related
I have submenu elements that has on click, slide out then spread out to fill a space. On the second click I'd like the animation to reverse before sliding in, but my jquery knowledge isn't good enough to achieve this. Can anyone help please?
my js:
$('.box').click(function(){
var flag = $(this).data('flag');
$('.tab1').toggle("slide", { direction: "left"}, 500);
$('.tab2').toggle("slide", { direction: "left" }, 500);
$('.tab3').toggle("slide", { direction: "left" }, 500);
$('.tab1').animate({top: (flag ? '+=50px' : '-=50px')});
$('.tab3').animate({top: (flag ? '-=50px' : '+=50px')});
$(this).data('flag', !flag)
});
JSFiddle
The animations for an element run after the previous one has finished, so at the moment the left slides will always run and when that has finished, the vertical animation kicks in.
When flag is true, you want the vertical animation to run first. So you need to do the vertical animation first.
The other issue then is that you are not animating .tab2 in the vertical animation, so it starts shrinking too early and looks odd. You can get around this by animating it by 0px during the vertical animation, so it will wait until the correct time to shrink:
$('.box').click(function(){
var flag = $(this).data('flag');
if(flag) {
$('.tab1').animate({top: '+=50px'});
$('.tab3').animate({top: '-=50px'});
$('.tab2').animate({top: '+=0px'});
}
$('.tab1, .tab2, .tab3').toggle("slide", { direction: "left"}, 500);
if(!flag) {
$('.tab1').animate({top: '-=50px'});
$('.tab3').animate({top: '+=50px'});
}
$(this).data('flag', !flag)
});
.square{
margin-left:100px;
}
.box{
position:relative;
width:150px;
height:150px;
background-color:transparent;
color:#fff;
text-align:center;
}
.tab4{
position:absolute;
right:10px;
top:50px;
width:70px;
height:40px;
background-color:grey;
}
.tab{
width:70px;
height:40px;
background-color:grey;
display:none;
}
.tab1{
position:absolute;
right:-70px;
top:50px;
}
.tab2{
position:absolute;
right:-70px;
top:50px;
}
.tab3{
position:absolute;
right:-70px;
top:50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<div class="square">
<div class="box">
<div class="tab4">CLICK</div>
<div class="tab1 tab"></div>
<div class="tab2 tab"></div>
<div class="tab3 tab"></div>
</div>
</div>
This is just an ordering problem. Try this out:
function slide(){
$('.tab1').toggle("slide", { direction: "left"}, 500);
$('.tab2').toggle("slide", { direction: "left" }, 500);
$('.tab3').toggle("slide", { direction: "left" }, 500);
}
function expand(flag){
$('.tab1').animate({top: (flag ? '+=50px' : '-=50px')}, 500);
$('.tab3').animate({top: (flag ? '-=50px' : '+=50px')}, 500);
}
$('.box').click(function(){
var flag = ($(this).data('flag')!=undefined)?$(this).data('flag'):true;
if(flag){
slide();
expand(flag);
}
else{
expand(flag);
setTimeout(slide, 500);
}
$(this).data('flag', !flag)
});
I have updated your Jsfiddle:
http://jsfiddle.net/VDesign/k52c9h12/5/
JS Code
$('.box').click(function(){
if($(this).hasClass('open'))
{
$('.tab1').animate({top: '-=50px'});
// add callback function to wait untill tab1 and 3 are back to 0
$('.tab3').animate({top: '+=50px'}, function() {
$('.tab1').toggle("slide", { direction: "left"}, 500);
$('.tab2').toggle("slide", { direction: "left" }, 500);
$('.tab3').toggle("slide", { direction: "left" }, 500);
});
$(this).removeClass('open');
} else {
$('.tab1').toggle("slide", { direction: "left"}, 500);
$('.tab2').toggle("slide", { direction: "left" }, 500);
$('.tab3').toggle("slide", { direction: "left" }, 500);
$('.tab1').animate({top: '+=50px'});
$('.tab3').animate({top: '-=50px'});
$(this).addClass('open');
}
});
I found this old post asking for a simple fade image rotator:
Looking for a simple fade javascript image rotator
and found what I was looking for:
$(function() {
$('#fader img:not(:first)').hide();
$('#fader img').css('position', 'absolute');
$('#fader img').css('top', '0px');
$('#fader img').css('left', 'auto');
$('#fader img').css('right', 'auto');
//$('#fader img').css('left', '50%');
$('#fader img').each(function() {
var img = $(this);
$('<img>').attr('src', $(this).attr('src')).load(function() {
//img.css('margin-left', -this.width / 2 + 'px');
});
});
var pause = false;
function fadeNext() {
$('#fader img').first().fadeOut().appendTo($('#fader'));
$('#fader img').first().fadeIn();
}
function fadePrev() {
$('#fader img').first().fadeOut();
$('#fader img').last().prependTo($('#fader')).fadeIn();
}
$('#fader, #next').click(function() {
fadeNext();
});
$('#prev').click(function() {
fadePrev();
});
$('#fader, .arwBtn').hover(function() {
pause = true;
},function() {
pause = false;
});
function doRotate() {
if(!pause) {
fadeNext();
}
}
var rotate = setInterval(doRotate, 4000);
});
this was the original fiddle
http://jsfiddle.net/jtbowden/UNZR5/1/
I have already spent some time adapting it (not a jquery programmer :/)
And all I need to add are captions to each picture.
Can someone help me just add captions to that same code?
Really appreciate it : )
You will need to enhance CSS more than this, but it is working as requested
http://jsfiddle.net/dU93C/
CSS
#fader {
position: relative;
width:500px;
height: 400px;
}
#fader div{
position: relative;
height: 400px;
width:400px;
overflow:hidden;
margin:0 auto;
}
#fader strong{
display:block;
position: absolute;
width:100%;
top:0;
left:0;
Background:#333;
Color:#fff;
padding:5px;
opacity:0.7;
filter:alpha(opacity=70);
}
.button {
background-color: green;
width: 50px;
height: 30px;
font-size: 20px;
line-height: 30px;
text-align: center;
position: absolute;
top: 30px;
}
#next {
right: 0;
}
#prev {
left: 0;
}
HTML
<div id="fader">
<a class="button" href="#" id="next">Next</a>
<a class="button" href="#" id="prev">Prev</a>
<div>
<strong>Image 1</strong>
<img src="http://dummyimage.com/600x400/000/fff.png&text=Image1"/>
</div>
<div>
<strong>Image 2</strong>
<img src="http://dummyimage.com/200x400/f00/000.jpg&text=Image2"/>
</div>
<div>
<strong>Image 3</strong>
<img src="http://dummyimage.com/100x100/0f0/000.png&text=Image3"/>
</div>
<div>
<strong>Image 4</strong>
<img src="http://dummyimage.com/400x400/0ff/000.gif&text=Image4"/>
</div>
<div>
<strong>Image 5</strong>
<img src="http://dummyimage.com/350x250/ff0/000.png&text=Image5"/>
</div>
</div>
JS
$(function() {
$('#fader div:not(:first)').hide();
var pause = false;
function fadeNext() {
$('#fader div').first().fadeOut().appendTo($('#fader'));
$('#fader div').first().fadeIn();
}
function fadePrev() {
$('#fader div').first().fadeOut();
$('#fader div').last().prependTo($('#fader')).fadeIn();
}
$('#fader, #next').click(function(e) {
fadeNext();
e.preventDefault()
});
$('#prev').click(function() {
fadePrev();
});
$('#fader, .button').hover(function() {
pause = true;
},function() {
pause = false;
});
function doRotate() {
if(!pause) {
fadeNext();
}
}
var rotate = setInterval(doRotate, 2000);
});
I'm trying to reproduce this and it's not working:
http://www.templatemonster.com/demo/44543.html
Here is my result: http://webs-it.com/callstar/
What I want is to navigate through the menu like the example I posted. I'm kinda new to javascript and I didn't manage to make it work.
Here is my code:
<html>
<head>
<link rel="stylesheet" href="css/style.css" type="text/css" >
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<style>
#block1 {
float:left;
width:1058px;
height:0px;
margin-left:-1058px;
margin-top:100px;
background-color:#fff;
position:relative;
}
#block2 {
width:1058px;
height:0px;
margin-left:-1058px;
margin-top:100px;
background-color:#fff;
position:relative;
}
#logo {
margin:0 auto;
width:502px;
height:259px;
margin-top:144px;
}
</style>
</head>
<body>
<div id="continut">
<div id="header">
<div id="social">
<img src="images/social/facebook.png" name="facebook" alt="."> <img src="images/social/ytube.png" name="ytube" alt="."> <img src="images/social/en.png" name="en" alt="."> <img src="images/social/cz.png" name="cz" alt=".">
</div>
</div>
<div id="block1">test test</div>
<div id="block2">test test</div>
<div id="logo" >
<img src="images/logo/logo_homepg.png">
</div>
<div id="meniu">
<img src="images/meniu/about.png" id="go1" name="about" alt="."><img src="images/meniu/photo.png" name="foto" id="go2" alt="."><img src="images/meniu/video.png" id="go3" name="video" alt="."><img src="images/meniu/ref.png" name="ref" id="go4" alt="."><img src="images/meniu/contact.png" name="contact" id="go5" alt=".">
</div>
</div>
<div id="footer"></div>
<script>
$( "#go1" ).click(function(){
$( "#block1" ).animate({ height: "300px" }, 1 )
.animate( { margin: "100px 0px" }, { queue: false, duration: 700 });
});
$( "#go2" ).click(function(){
$( "#block2" ).animate({ height: "300px" }, 1 )
.animate( { margin: "100px 0px" }, { queue: false, duration: 700 });
$( "#block1" ).animate({ height: "300px" }, 1 )
.animate({ margin: "100px 1558px" }, { queue: false, duration: 700 });
});
</script>
</body>
That was a fun challenge.
I created a similar effect for you here.
Check out the jsFiddle DEMO
P.S: All, HTML, CSS, and Javascript is new.
HTML
<div id="container">
<div id="content">
<div id="one"><p>One</p></div>
<div id="two"><p>Two</p></div>
<div id="three"><p>Three</p></div>
<div id="four"><p>Four</p></div>
</div>
<ul id="nav">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
</div>
CSS
*{margin:0; padding:0; font-family: Helvetica}
#container{
width:100%;
height:600px;
overflow:hidden;
float:left;
background: url(http://webs-it.com/callstar/images/covers/electric_guitar2.jpg) center no-repeat;
}
p {
font-size:25px;
text-transform:uppercase;
text-align:center;
font-weight:bold;
color:#666;
margin-top:22%;
}
#content {
width:100%;
height:200px;
margin: 50px auto 0;
position:relative;
overflow:hidden;
}
#content > div {
display:block;
width:400px;
height:200px;
background:white;
position:absolute;
margin-left:100%;
/*left:-200px;*/
opacity: 0;,
top:40px
}
#nav {
list-style:none;
display:table;
margin:0 auto;
position:relative;
}
#nav li {
display:table-cell;
align:cen
display:inline-block;
padding:20px
}
#nav a {color:#fff; text-decoration:none;}
JS
$(function() {
var content = $('#content'),
contentHeight = content.height(),
nav = $('#nav'),
count = 0;
// on load content height is shorter
content.height(100);
nav.find('a').on('click', function() {
var $this = $(this),
parent = $this.parent(),
targetElement = $this.attr('href');
//Does the slide down animation once
if (count === 0) {
content.animate({'height': contentHeight }, function() {
parent.addClass('active');
//animate in
$(targetElement).animate({
left: '-=200px',
'margin-left': '50%',
opacity: 1
}, 500);
});
count = 1;
} else {
//only add active class if parent does not have it and then animate it
if ( !parent.hasClass('active') ) {
parent.addClass('active');
//animate in
$(targetElement).animate({
left: '-=200px',
'margin-left': '50%',
opacity: 1
}, 500);
//Gets older clicked element
var oldClickedElement = $('.active').not(parent).removeClass('active').find('a').attr('href');
//only if old click element exists the do the animation
if (oldClickedElement) {
//animate out + reset to start
$(oldClickedElement).animate({
left: 0,
'margin-left': '-50%',
opacity: 0
}, 500, function() {
$(oldClickedElement).css({
'margin-left' : '100%',
left: 0
});
});
}
}
}
return false;
});
});
Let me know if you have any questions.
I have put together this jsfiddle.
http://jsfiddle.net/YJPV9/
<div id="wrapper">
<div class="portfolio"></div>
<div class="portfolio"></div>
<div class="portfolio"></div>
<div class="portfolio"></div>
</div>
.portfolio {
height:50px;
width:200px;
float:left;
margin:20px 20px 0 0;
background-color:red;
}
#wrapper {
width:500px;
}
.portfolio:hover {
}
$('.portfolio', '#wrapper').hover(function() {
$('.portfolio', '#wrapper').not(this).stop().animate({
opacity: .2
}, 500);
$(this).stop().animate({
opacity: 1
}, 500);
});
$('.portfolio', '#wrapper').mouseleave(function() {
('.portfolio', '#wrapper').animate({opacity:1}, 500);
});
I'm looking to return all of the elements back to opacity: 1 when the mouse leaves the wrapper.
Try this:
$('.portfolio', '#wrapper').hover(function () {
$('.portfolio', '#wrapper').not(this).stop().animate({
opacity: .2
}, 500);
}, function (){
$('.portfolio', '#wrapper').stop().animate({
opacity: 1
}, 500);
});
jsFiddle example
There was a $ missing in your Fiddle. Adding that allowed it to work as intended.
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');
}
});
}
});