I currently have the following code.
to get the #headerfader to resume running after the second function runs (hovering over and off again to reset the fading text)
I've duplicated the code inside the function.
Is there a more elegant way of handling this?
//FADER TEXT
$('#headerFader').carousel({
interval: 2500,
pause: false
});
//BACK HOME TEXT
$('#headerText').hover(
function(){
$(this).find("h1#masterHeader").animate({opacity:0}, 0, function(){
$(this).css("display", "none");
$('#headerText').find("h1#takemeback").css("display", "block");
$('#headerText').find("h1#takemeback").animate({opacity:1,});
});
},
function(){
$(this).find("h1#takemeback").animate({opacity:0}, 0, function(){
$(this).css("display", "none");
$('#headerText').find("h1#masterHeader").css("display", "block");
$('#headerText').find("h1#masterHeader").animate({opacity:1,});
//FADER TEXT
$('#headerFader').carousel({
interval: 2500,
pause: false
});
});
}
);
This is a complex solution to this problem, which minimizes the numbers of jQuery element fetching calls
Note that it's clean and a lot faster than the other solutions provided. Unless it's tested I believe that this is a working piece of code.
Since you are using # div id selector (which is unique) the is no need for calling find() function.
//Object handles
var headerFader = $('#headerFader');
var masterHeader = $('#masterHeader');
var takemeback = $('#takemeback');
headerFader.carousel({
interval: 2500,
pause: false
});
//BACK HOME TEXT
headerFader.hover(
function(){
masterHeader.animate({opacity:0}, 0, function(){
$(this).css("display", "none");
takemeback.css("display", "block").animate({opacity:1}, 1000);
});
},
function(){
takemeback.animate({opacity:0}, 0, function(){
$(this).css("display", "none");
masterHeader.css("display", "block").animate({opacity:1}, 1000);
headerFader.carousel({
interval: 2500,
pause: false
});
});
}
);
Outside your other functions, you'd do this:
function showHeader(el) {
$(el).css("display", "none");
$('#headerText').find("h1#takemeback").css("display", "block");
$('#headerText').find("h1#takemeback").animate({opacity:1,});
}
Then, inside your hover functions, call it:
showHeader(this);
Along with the variable suggestion above, you can chain your statements:
$('#headerText').find("h1#takemeback").css("display", "block").animate({opacity:1,});
Also, #takemeback should be unique, so this would do:
$("#takemeback").css("display", "block").animate({opacity:1,});
Related
I know this might be silly but I would like to know if there is a way to realize.
Basically, I would like the dropdown-content element to 'KEEP DISPLAYING' even after 3 secs of mouse moving-out of the parental 'dropbtn' button or element.
E.g. code:
$(function() {
$('#dropbtn').hover(function() {
$('.dropdown-content').css('display', 'block');
}, function() {
// on mouseout:
setTimeout(function(){$('.dropdown-content').css('display', 'none');}, 3000);
});
$('.dropdown-content').hover(function(){
$('.dropdown-content').css('display', 'block');
},function(){
$('.dropdown-content').css('display', 'none');
})
});
Current issue is that setTimeout() function is overriding my desired way on this particular line of JS code:
$('.dropdown-content').css('display', 'block');
In another word, I want setTimeout() to be effective if and only if I set not my mouse cursor on 'dropdown-content' div.
Hope someone can help out :)
Instead of using hover, you could use mouseenter/mouseleave to 'toggle' the .dropdown-content, except the delay of 3s on mouseleave:
$(function() {
var dropdownTimeout = null;
$('#dropbtn').mouseenter(function() {
if(dropdownTimeout) {
clearTimeout(dropdownTimeout);
dropdownTimeout = null;
}
$('.dropdown-content').css('display', 'block');
});
$('#dropbtn').mouseleave(function() {
dropdownTimeout = setTimeout(function(){$('.dropdown-content').css('display', 'none');}, 3000);
});
});
I've got a following code for fadeOut, load another content and fadeIn, but I've got a problem, that sometimes, when the load function is very fast, it switches the loaded content even before the timeline completely fadeOut, so the effect is a bit weird at this case. How can I prevent this?
Note
I want to load content immediately after click, so putting the load function into the first fadeTo callback function is not the solution. Thanks!
$(".switches li").click(function(evn) {
$(".switches li").removeClass("active");
$(evn.target).addClass("active");
$(".timeline").fadeTo(400, 0, function(){
$(this).css("visibility", "hidden");
});
$(".timeline").load("inc-timeline/"+evn.target.id+".html", function() {
$(this).fadeTo(400, 100, function() {
$(this).css("visibility", "visible");
if(evn.target.id === "data-girls") {
$(".data-girls-powered").fadeIn(400);
} else {
$(".data-girls-powered").fadeOut(400);
}
});
});
});
Use start option of .animate(), .finish()
// call `.load()` when `.fadeTo(400, 0)` starts
$(".timeline").finish().animate({opacity:0},{
start: function() {
// do asynchronous stuff; e.g., `.load()`
$(this).load("inc-timeline/"+evn.target.id+".html", function() {
// stop `.fadeTo(400, 0)` animation,
// start animating to `opacity:1`
$(this).finish().fadeTo(400, 1, function() {
// do stuff
});
});
},
duration: 400
});
$("button").click(function() {
// call `.load()` when `.fadeTo(400, 0)` starts
$(".timeline").finish().animate({opacity:0},{
start: function() {
var el = $(this);
// do asynchronous stuff; e.g., `.load()`
$.Deferred(function(dfd) {
setTimeout(function() {
dfd.resolveWith(el)
}, Math.floor(Math.random() * 3500))
}).promise().then(function() {
// stop `.fadeTo(400, 0)` animation,
// start animating to `opacity:1`
$(this).finish().fadeTo(400, 1, function() {
});
});
},
duration: 400
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>click</button>
<div class="timeline">abc</div>
What about changing the duration of the load..to be longer than 400 milliseconds, would that help?
I am currently using the following code to initialize a lazy initialization version of Bootstrap tooltip. After the first hover everything works fine in regards to the delay, but on the initial hover it shows right away. I know this is because of the $(this).tooltip('show'); method, but I dont know how to use the delay and show at the same time. I have to use the $(this).tooltip('show'); because once hovered the element doesnt show the tooltip unless I move out and back in.
$(element).on('hover', '.item', function () {
matchup = ko.dataFor(this).Matchup;
if (matchup) {
if ($(this).attr('data-original-title') != '') {
$(this).tooltip({ title: matchup.Title, html: true, delay: 1000 });
$(this).tooltip('show');
}
}
});
Updated Answer
$(element).on('mouseenter', '.item', function (e) {
matchup = ko.dataFor(this).Matchup;
if (matchup) {
if ($(this).attr('data-original-title') != '') {
$(this)
.addClass('tooltip-init')
.tooltip({ title: matchup.Title, html: true, delay: { show: 1000, hide: 0 } })
.trigger(e.type);
}
});
try use trigger
try the following code
$(this).tooltip({
title: matchup.Title,
html: true,
trigger: 'hover',
delay: delay: { show: 2000, hide: 3000 }
}).trigger('hover');
I found Holmes answer using delay to work, but not reliably. When moving through a series of items, the hover seemed to stop showing. With the help of another stackoverflow answer leading to this jsfiddle by Sherbrow, I simplified the code and got it working in this jsfiddle. Simplified code below:
var enterTimeout = false;
$('[rel="tooltip"]').tooltip({trigger:'manual'}).on('mouseenter', function() {
var show = function(n) {
enterTimeout = setTimeout(function(n) {
var isHovered = n.is(":hover");
if (isHovered) n.tooltip('show');
enterTimeout = false;
}, 750);
};
if(enterTimeout) clearTimeout(enterTimeout);
show( $(this) );
});
$('[rel="tooltip"]').on('mouseout click',function() {
$(this).tooltip('hide');
});
I'm trying to make it so that when you click a link, it removes a div (with some paragraphs and text) and inserts another div (with some paragraphs and some text). I'm using jQuery to fade those in and out. The fading out of the original div works when you click the link, and then I have a switch case to determine what gets faded in. However, the fadeIn, set to 'slow', appears to be occurring immediately.
Here's the relevant piece of code (the rest is just other cases):
$(document).ready(function() {
$('.nav-link').click(function() {
var linkClicked = $(this).attr("id");
$('.content').fadeOut('fast');
switch(linkClicked) {
case 'home':
console.log("linkClicked = "+linkClicked);
$('#home-content').fadeIn('slow', function() {
$(this).css("display", "inline");
$(this).css("opacity", 100);
});
break;
Edit:
So after changing fadeTo to fadeOut, and changing "slow" in the fadeOut to "fast", it worked well and transition the way I want. However, whenever I click "home" now it will move the div to a "block" position, (it spits it to the lower left corner) before shoving itself back into the right spot in the center of my container. It ONLY does this when I click home and no other of my sidenav links...which are all running the exact same code (home just is the first one in the switch case). Any ideas?
If you want the fadeIn to start after the fadeTo has completed, you'll want to use a callback function. Also, since you're fading to 0 opacity, just use fadeOut:
$(document).ready(function() {
$('.nav-link').click(function() {
var linkClicked = $(this).attr("id");
$('.content').fadeOut('slow', function() {
// this code will begin once fadeTo has finished
switch(linkClicked) {
case 'home':
console.log("linkClicked = "+linkClicked);
$('#home-content').fadeIn('slow', function() {
$(this).css("display", "inline");
$(this).css("opacity", 100);
});
break;
});
});
Without seeing your HTML, it's a little difficult to understand the exact outcome you're trying to achieve, but here is a JSfiddle with your code above.
http://jsfiddle.net/W9d6t/
$('.nav-link').click(function() {
var linkClicked = $(this).attr("id");
//$('.content').fadeTo('slow', 0);
switch(linkClicked) {
case 'home':
console.log("linkClicked = "+linkClicked);
$('#home-content').fadeIn('slow', function() {
$(this).css("display", "block");
alert('All done!');
});
}
});
From my understanding of what you are trying to do, I believe you simply need to do this:
$('#home-content').fadeIn('slow');
(the fadeIn function automatically sets the display property to inline/block)
Also, while your implementation correct, it's simpler to do:
$('.content').fadeOut('slow');
(simplified jsFiddle)
You just need to add a callback to fadeOut so that it executes after the animation is done:
$(document).ready(function() {
$('.nav-link').click(function() {
var linkClicked = $(this).attr("id");
$('.content').fadeOut('slow', function() {
switch(linkClicked) {
case 'home':
console.log("linkClicked = "+linkClicked);
$('#home-content').fadeIn('slow', function() {
$(this).css("display", "inline");
$(this).css("opacity", 100);
});
break;
});
im a very very noobie in jquery question, im developing a site using maximage plug in, it seem every work perfectly, but i dont know how to hightlight a link when is click it on it, sorry for my terrible english.
Here's the website i'm developing.
http://aranasoluciones.com/azulejera/ejemplo-ok.html
and there is the js
<script type="text/javascript" charset="utf-8">
$(function(){
$('#maximage').maximage({
cycleOptions: {
fx: 'fade',
speed: 3000, // Has to match the speed for CSS transitions in jQuery.maximage.css (lines 30 - 33)
timeout: 0,
prev: '#arrow_left',
next: '#arrow_right',
},
onFirstImageLoaded: function(){
jQuery('#cycle-loader').hide();
jQuery('#maximage').fadeIn(1500);
jQuery('.in-slide-content').delay(1200).fadeIn('slow');
}
});
});
$('.toggle').bind('click', function(e){e.preventDefault();
$('#maximage').cycle('toggle');
});
$("#2").click(function(e) {
$('#maximage').cycle(1);
return false;
});
$("#1").click(function(e) {
$('#maximage').cycle(0);
return false;
});
$("#3").click(function(e) {
$('#maximage').cycle(2);
return false;
});
</script>
PLEASE HELP!!!!! i will preaciated very much!!!
This would do what you need:
$("#nav li a").click(function () {
$("#nav li a").removeClass("selected"); // OR $("#nav li a").css("background-color");
this.addClass("selected"); // OR $("#nav li a").css("background-color","#0070ba");
});
However would suggest you to change your navigation link id to something more meaningful (instead of #1 #2 #3 to #nav1 #nav2 #nav3).
If i use the code
$("#maximage").cycle(1);
$("#maximage").cycle(2);
explicitly in your website specified using firebug the image getting changed. Could you please check whether the button with id "#1","#2" are defined and event defined for it are correct and the definition for click event should be defined inside $(function(){ }); or document ready. Please check the below code.
<script type="text/javascript" charset="utf-8">
$(function(){
$('#maximage').maximage({
cycleOptions: {
fx: 'fade',
speed: 3000, // HastomatchthespeedforCSStransitionsinjQuery.maximage.css(lines 30 - 33)
timeout: 0,
prev: '#arrow_left',
next: '#arrow_right',
},
onFirstImageLoaded: function(){
jQuery('#cycle-loader').hide();
jQuery('#maximage').fadeIn(1500);
jQuery('.in-slide-content').delay(1200).fadeIn('slow');
}
});
});
$('.toggle').bind('click', function(e){e.preventDefault();
$('#maximage').cycle('toggle');
$("#2").click(function(e) {
$('#maximage').cycle(1);
return false;
});
$("#1").click(function(e) {
$('#maximage').cycle(0);
return false;
});
$("#3").click(function(e) {
$('#maximage').cycle(2);
return false;
});
});
</script>
Thanks