:D
I am currently having much trouble with a jQuery animation. Basically, a button click will quickly start a short animation and collapse a sidebar, widening the main content box to full width (and back again if wanted). The issue is that with quick consecutive clicks, the layout goes all crazy. I have tried this condition:
if (!$(this).is(":animated"))
{
// Code
}
But it doesn't work. So I have tried .off(), and it shuts off, but I cannot find out how to turn it back .on(). Can someone help me please? Here is what I have:
<script type="text/javascript">
$(document).ready(function(){
var $button = $("a#toggle");
var $content = $("div#index_main-content");
var $sidebar = $("div#sidebar");
// Disable quicky clicky
$button.click(function() {
$button.off().delay(1000).on();
});
// Hide sidebar
$button.toggle(function sidebarToggle() {
$sidebar.fadeOut(500, function() {
$content.animate({width: '100%'}, 500, function() {
$button.attr("title", "Click to show the sidebar!").addClass("hiding").removeClass("showing");
});
});
},
// Show sidebar
function sidebarToggle() {
$content.animate({width: '69.5%'}, 500, function() {
$sidebar.fadeIn(500, function() {
$button.attr("title", "Click to hide the sidebar!").addClass("showing").removeClass("hiding");
});
});
});
});
</script>
<div id="index_content">
<a title="Click to hide the sidebar" class="showing" id="toggle"></a>
<div id="sidebar">
<!-- Sidebar: float-right/width-28.5% -->
</div>
<div id="index_main-content">
<!-- Content: float-left/width-69.5% -->
</div>
</div>
Also, there is a live demo here. Like I said before, for some reason, the .on() does not happen. :(
Thank you. :)
$content.stop(true,true).animate({ //code });
Try using stop before issuing the animation the second time, for example:
$content.stop().animate(
This will stop and previous animations before starting the new one.
Also use true in the stop statement to cancel other animation and complete the animations.
$content.stop(true,true).animate(
See:
http://api.jquery.com/stop/
stop() to stop the current animation,
clear the animation queue and go to the end of the animation .stop(true,true)
or
turn the button to OFF before you start the animation
turn the button to ON within the animation callback function, so that
is turned on again after the animation finished
OR more easy
<div id="index_content">
<a title="Click to hide the sidebar" class="showing" id="toggle">Click me</a>
<div id="sidebar">sidebar
<!-- Sidebar: float-right/width-28.5% -->
</div>
<div id="index_main-content">content
<!-- Content: float-left/width-69.5% -->
</div>
</div>
$(document).ready(function(){
$('#toggle').click(function(){
$('#sidebar:visible').fadeOut('slow')
$('#sidebar:hidden').fadeIn('slow')
})
})
take a look#
http://jsfiddle.net/jdFrR/
Related
I want to disable register button and change text to "Please wait..." after user click register button. I tried using javascript like,
HTML,
<button id="disable-submit" type="submit" class="g-recaptcha btn btn-light rounded-pill" data-sitekey="{{ config('services.recaptcha.sitekey') }}" data-callback='onSubmit' data-action='submit'>
{{ __('Register') }}
</button>
js,
<script type="text/javascript">
// $(document).ready(function()
(function(){
$('#disable-submit').on('submit', function(){
var btnd = $('#disable-submit');
btnd.prop('disabled', true);
setTimeout(function(){
btnd.prop('disabled', false);
}, 5000);
})
})();
</script>
Now it disables for a second and gets re-enable. I think it re-enables because of validation checks. I need to disable the button for 4-5 seconds, my registration process needs that much time.
I have added this scroll feature with no plugins to scroll up and down a gallery of large images on a single page. My code is not very elegant but the scroll basically repeats itself from image to image on action of click on arrows.
<script>
$(document).ready(function (){
$("#click4").click(function (){
$('html, body').animate({
scrollTop: $("#image2").offset().top
}, 600);
});
});
</script>
<script>
$(document).ready(function (){
$("#click5").click(function (){
$('html, body').animate({
scrollTop: $("#image4").offset().top
}, 600);
});
});
</script>
<div id="top">
<class id="image3"><img src="images/blank.png" alt=""/></class></div>
<div id="gallery">
<img src="images/image3.jpg" alt=""/>
</div>
<div id="title">
Untitled<br>2013<br>size<br>Archival pigment print<br>
Edition of <br></div>
<div id="arrows">
<class id="click4"> <span class="arrow-n"></span> </class>
</div>
<div id="arrows">
<class id="click5"> <span class="arrow-s"></span> </class>
</div>
<script>
$(document).ready(function (){
$("#click6").click(function (){
$('html, body').animate({
scrollTop: $("#image3").offset().top
}, 800);
});
});
</script>
<script>
$(document).ready(function (){
$("#click7").click(function (){
$('html, body').animate({
scrollTop: $("#image5").offset().top
}, 800);
});
});
</script>
<div id="top">
<class id="image4"><img src="images/blank.png" alt=""/></class> </div>
<div id="gallery">
<img src="images/image4.jpg" alt=""/>
</div>
<div id="title">
Untitled<br>2013<br>size<br>Archival pigment print<br>
Edition of <br></div>
<div id="arrows">
<class id="click6"> <span class="arrow-n"></span> </class>
</div>
<div id="arrows">
<class id="click7"> <span class="arrow-s"></span> </class>
</div>
The problem I am having is the images seem to flicker as you move up and down. Two questions.
Is there a simple way to avoid flickering with large images when scrolling.
Can I add a fade in fade out as you scroll into each image and how do I add this to the script. I think this will reduce the amount of flickering and add a nice effect.
Thanks for any advice.
Ive taken a look at what you have posted but I'm not able to get your code working as-is.
firstly, you should try to combine your scripts into 1. for example, you have a function for each click to scroll to 1 image. What you should do is create just 1 function then have that check the image to scroll to. There are many ways that you can do this but here is an example:
$(".clk").click(function () {
var $this = $(this),
img = $this.data('img');
$('html, body').animate({
scrollTop: $("#"+img).offset().top
}, 600);
});
Along with this function, you would need to change your markup like this:
<div id="Div6">
<div id="click7" data-img="image5" class="clk"> <span class="arrow-s"></span>
</div>
</div>
so, I have added a class of clk, this is what is going to call the function now, not clicking on the ID so that we can add this class to as many images as we want. Next, in your functions you are scrolling to the top of an image which is hard coded into each ID function... I have added an HTML Data attribute "data-" and have called it img ("data-img"). So now, when you click on class, you will call the function which will read the attribute and know where to scroll to.
Once you have got this working, please create a jsFiddle (jsfiddle.net) where you paste your html, jquery and css into the boxes and you can run the code there. Then save it and post the link back here for me so that I can see a working version and I'll take another look for you. ;-)
HTML:
<a class="targetLink" href="#">LINK1</a>
<div id="text1" style="display: none;">text1 div</div>
<a class="targetLink" href="#">LINK2</a>
<div id="text2" style="display: none;">text2 div</div>
<a class="targetLink" href="#">LINK3</a>
<div id="text3" style="display: none;">text3 div</div>
JS:
$("a.targetLink").toggle(function() {
$(".open").slideUp(350);
$(this).next("div").slideDown(350).addClass("open");
}, function() {
$(this).next("div").slideUp(350).removeClass("open");
});
It works this way: when u press a link with class "targetLink" it opens a DIV below it. Now i need to modify js code to: when i click then link it scrolls to the beginning of that opened div. How can i achieve it? Thanks in advance.
Use scrollTop on the element that you want to scroll to the top of. http://api.jquery.com/scrollTop/
example:
$(this).next("div").scrollTop(0);
I think this is what you need, and maybe more: My jsfiddle. I'm not sure what you've got working so far, but this does what i think you're going for, without the need for additional jquery plugins, etc.
here's the JS(check the jsfiddle for html):
$(".pop").hide();
$(".targetLink").on("click", function(e) {
e.preventDefault();
var n = $(this).next();
if(!$(n).hasClass('open')){
$(".open").removeClass('open').slideUp(200);
$(n).addClass('open').slideDown(200);
}
});
$(".nav").on("click", function(event){
event.preventDefault();
var dest=null;
if(($($(this.hash)).offset().top) > ($(document).height()-$(window).height())){
dest= $(document).height()-$(window).height();
}else{
dest=$($(this.hash)).offset().top;
}
$($(this.hash)).trigger("click");
$('html,body').animate({scrollTop:dest}, 500, 'swing' );
});
Try this really great and simple jQuery plugin - https://github.com/Ashwell/jquery-scrollThis
This should do what you're asking for:
self.scrollToDiv = function scrollToDiv(element,minus){
element = element.replace("link", "");
if(minus==null){
minus=0;
}
$('html,body').unbind().animate({scrollTop: $(element).offset().top+minus},'slow');
};
So I've got an image that inside a tag, and I've also got a div that is centred in the middle of the screen.
How do I make this div invisible until the image is clicked?
Preview of page when the div is visible:
So that added to cart is a div in the middle of the screen. I want that to only show up when the "Add to cart" button has been clicked.
I want the div to go away again after 2 seconds. Transitions would be nice (eg fade into visibility), but I don't mind.
Set the display of the div to none and do a onclick event for the image which sets the display of the div to block. I would recommend using JQuery to add in any transitions or animations.
Something like (without animations):
<div id="cart" style="display:none">
</div>
<img src="imgsource" onClick="document.getElementById('cart').style.display = 'block'"/>
Worked it out. Had to use jQuery.
In the < head> :
<script>
$(document).ready(function(){
$(".addtocart").click(function(){
$(".addedcartbg").fadeIn(500);
$(".addedcartbg").delay(1000);
$(".addedcartbg").fadeOut(500);
});
});
</script>
In the < body> :
<div class="addedcartbg" style="display:none"><div class="addedcart"> Successfully added to cart. </div></div>
css
.hide{
display:none;
}
html
<div class="hide">
// put content here
</div>
<img class="hide-show" src="site.com/image.png" alt=""/>
js
$(function(){
$('.hide-show').bind('click',function(){
$('.hide').fadeIn(500).delay(2000).fadeOut(500);
});
});
Hi so when someone visits my site i would like to appear the facebox with the message "Click [like-button] to enter or wait 20 seconds" and when the visitor clicks the like button or the 20s are gone,the facebox disappear.Also the close button and click outside the box closing should be removed. This is what i tried.
<script type="text/javascript">
var sec = 10
var timer = setInterval(function() {
$('#hideMsg span').text(sec--);
if (sec == -1) {
$('#hideMsg').fadeOut('fast');
clearInterval(timer);
}
}, 1000);
</script>
</head>
<body>
<div id="hideMsg" style="display:none;">
Click <iframe src="http://www.facebook.com/plugins/like.php?href=link&layout=standard&show_faces=false&width=450&action=like&font=trebuchet+ms&colorscheme=light&height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true"></iframe><br />
to enter or wait <span>10</span> Seconds!
</div>
<script type="text/javascript">
jQuery.facebox({ div: '#hideMsg' })
</script>
Download the facebox files from this repository:
https://github.com/thinknirmal/facebox
I have done significant changes to the script and so you can optionally specify not to have the regular closing options.
When initiating facebox through jQuery, do it the following way:
$.facebox({
div : '#content-div',
easyClose : false
});
When you set easyClose to false, all the usual closing options are disabled (close button, escape key and clicking on the background). The default value of easyClose is true.