I have a simple CSS created design. Upon click of an arrow-looking CSS div I have another design slide into the frame from left: -9999px.
I can't seem to figure out how to make that div susceptible to more than one click.
this is what i mean:
Imagine the design looks like an X. Upon the click of a div a Y slides in from the right. XY. When the user clicks the button again I want the Y to slide out, and continue the same path to the left. I have the following JS:
topleft.addEventListener('click', function(){
addLeft.classList.replace('out', 'slideLeft')
addRight.classList.replace('out', 'slideLeft')
})
Since i am learning JS I'm looking for a vanilla JS solution, without any frameworks/libraries.
Thanks all
topleft.addEventListener('click', function(){
if (addLeft.classList[1] == 'out'){
addLeft.classList.replace('out', 'slideLeft')
addRight.classList.replace('out', 'slideLeft')
} else if (addLeft.classList[1] == 'slideLeft') {
addLeft.classList.replace('slideLeft', 'slideOut')
addRight.classList.replace('slideLeft', 'slideOut')
setTimeout(function(){
addLeft.classList.replace('slideOut', 'out'),
addRight.classList.replace('slideOut', 'out')
}, 300)
}
})
Related
I'm trying to create some sort of info "Scroll Down" button (not clickable) which has to be visible while scroll bar is up on top, fade out when scroll down a few pixels and fade back in when up again.
So far I was able to create the arrow and the message so far as well as the fading part.
Here is a fiddle: https://jsfiddle.net/8b3jL7r0/1/
var btn = $("#button");
$(window).scroll(function() {
if ($(window).scrollTop() < 100) {
btn.addClass("show");
} else {
btn.removeClass("show");
}
});
var btn2 = $("#scrolltxt");
$(window).scroll(function() {
if ($(window).scrollTop() < 100) {
btn2.addClass("showx");
} else {
btn2.removeClass("showx");
}
});
The problem with is that the arrow and the info text 'Scroll Down' does not appear right from the beginning, you have to scroll down a bit so they appear on top and then everything works smooth. Any clue how to make them visible right from the first load of the code?
Any idea how could I transfer all this code into one single code module in WordPress and have it work exactly like in the fiddle ? Because I've tried to insert it but it seems to not work at all, nothing appears on the page, not the arrow nor the info text.
I just added the inital classes to both elements:
https://jsfiddle.net/4e2cafju/
<div id="button" class="show"></div>
<div id="scrolltxt" class="showx">scroll down</div>
for 2:
You should be able to put these elements directly into a template. You should be able to add the css to the style sheet. And you could lnk to an external JS file. That would probably be best practice. You could also put all the code into a single page. I'm not sure how your wordpress install / theme is set up.
I looked through this website for similar question, but couldn't find any.
So I've been working on a website with bootstrap3 for a little bit now and most of the formatting/design part is done, but have one problem I can't solve myself.
I added Jquery to make the navbar shrinks and changes background color when users scroll down to a certain point.(Thanks to peeps helped me out here)
It kind of works, but the movement of it is really weird.
When load the page, the navbar is already shrunk and background is colored, but when scrolled a little bit it blows up in size and the background disappears, and when scrolled even more to the point where I set Jquery to start working, navbar shrinks back and background color changes again.
It's hard to explain in writing, so please see the website and see what I'm talking about.
Below is the website I'm working on.
Test website
I'm assuming its the Jquery not working when loaded, so the CSS setting supposed to be hidden(shorter navbar height and background color) isn't hidden initially.
Below is the jquery code:
$(document).ready(function(){
$(window).scroll(function () {
if ($(window).scrollTop() > 70) {
$("#top-bar").addClass('animated-header');
} else {
$("#top-bar").removeClass('animated-header'); }
});
$("#clients-logo").owlCarousel({
itemsCustom : false,
pagination : false,
items : 5,
autoplay: true,
})
});
Thanks for the help in advance!
Nice website!
Take a look at your header element, you will see that you already put the animated-header class there which causes the problem. Here is your code:
<header id="top-bar" class="navbar-fixed-top animated-header">
What you can do is simply remove that class, and your script above will help deal with adding/removing this class base on the scrollTop value. Something like this will help:
<header id="top-bar" class="navbar-fixed-top"> <!-- without animated-header -->
We are setting our scroll position back to zero, so this works fine. Add this,
$(document).ready(function () {
$('html,body').animate({
scrollTop : 0
},10);
});
$(document).ready(function () {
$(window).scroll(function(){
if($(window).scrollTop() >= 70){
$("#top-bar").addClass("animated-header");
}
if($(window).scrollTop() <= 70){
$("#top-bar").removeClass("animated-header");
}
});
});
Coda Slider:
http://jqueryfordesigners.com/coda-slider-effect/
I recently implemented the Coda Slider on my webpage but I came across one issue. What the issue more like a implementation, I was wondering about is, change/disable the left button if the user is on the first slide and change/disable the right button if the user is on the right slide. User cannot press the left button to go to the last slide and the right button to scroll all the way to the first slide. I would like to disable that feature.
I tried to look at the code and see where I would have to change or add the code to but was unable to determine. Can anyone please help?
Example:
I have four slides (0-3) What code would I add so the left button (id=leftb) is hidden when I scroll to slides 0 and the right button (id=rightb) is hidden when I scroll to slide 3. I added the following code:
if (n<0) { //if on first slide make left button hidden and right button visible
if (document.getElementById) {
document.getElementById('leftb').style.visibility = 'hidden';
document.getElementById('rightb').style.visibility = 'visible';
}}
if (n==0 || n==1 || n ==2) { //if on second and third slides make both button visible
if (document.getElementById) {
document.getElementById('leftb').style.visibility = 'visible';
document.getElementById('rightb').style.visibility = 'visible';
}}
if (n>2) { //if on last slide make right button hidden and left button visible
if (document.getElementById) {
document.getElementById('leftb').style.visibility = 'visible';
document.getElementById('rightb').style.visibility = 'hidden';
}}
The above code i think makes sense in terms hiding and showing the navigation button based on the slide number but it's not working.
Any idea?
EDIT:
The files version i currently have are:
jquery-1.2.6.js
jquery.easing.1.3.js
jquery.localscroll-1.2.5.js
jquery.scrollTo-1.3.3.js
jquery.serialScroll-1.2.1.js
coda-slider.js
Which files am i downloading from the github and update ony my server for it to work properly?
I've forked serialScroll to add this feature. You now have a 'wrap' option that you can set to false to disable. You can check my commit on github to see the details:
https://github.com/lperrin/jquery.serialScroll
Basically, I added checkDisableWrap:
function checkDisableControls( pos, limit ){
$(settings.prev).css('visibility', pos <= 0 ? 'hidden' : 'visible');
$(settings.next).css('visibility', pos >= limit ? 'hidden' : 'visible');
};
Which is triggered every time the plugin scrolls.
I do not know how to solve this situation:
I`ve got the html/css looks like this:
Image showing how my css/html looks like and what is displayed on the screen after landing on page:
The when I scroll down I see green element:
scrolling down ->
After continuing to scrolling down I saw full green element and the if I scroll down I want to have this element like in css language: position fixed bottom 0. See image below:
I ve saw full element -> same link but image called problem3.png
and then I scroll below and I want to have it fixed at the bottom of the page, like on this image:
Fixed element on screen - What I want and I do not know how to do that -> same link but image called problem4.png (stupid spam prevention mechanism)
Is it possible to solve this situation ?
To sum up: I`ve got two divs, one above and second below, Wheen I scroll down I suddenly see another element (green div) and when i continue to scroll down I WANT TO HAVE THIS GREEN DIV FIXED AT THE BOTTOM OF THE PAGE.
Ofcourse, when I scroll up (back on the top) I want to "park" that green div at the top of the second div.
Is there any way to solve this situation with jQuery (Javascript) / html / css ?
Thank you in advance
I think you'll have to show some of your html structure. There are lots of ways to achieve this kind of effect. Fundamentally, in javascript terms you'll be looking to:
Add an event listener to the window scroll that checks whether the green element is fully in view
If it is in view, add a class (or change it's css) that fixes it's position where you want
Change your window scroll method so that it's checking the relative offset of the red div to the top of the screen. If it goes below the position where the green div should be fixed, remove the class you added earlier.
That sounds complicated, but it's not too bad. The javascript would be something like:
$(function() {
$(window).scroll(function() {
if($(".divToFix").hasClass("fixedAtBase")){
if(Utils.underView($(".redDiv"), $(".divToFix").height())) $(".divToFix").removeClass("fixedAtBase");
} else {
if(Utils.inView($(".divToFix"))) $(".divToFix").addClass("fixedAtBase");
}
});
});
Utils = {
underView: function(element, offset) {
return (($(window).height() + $(window).scrollTop() - offset) <= element.offset().top);
},
aboveView: function(element) {
return ($(window).scrollTop() >= element.offset().top + element.height());
},
inView: function(element) {
return (Utils.aboveView(element) !== true && Utils.underView(element, element.height()) !== true);
}
};
Bear in mind I've not tested that or anything.
edit - here's a demo
I'm trying to hide a div on click (well i'm trying to slide it to the left), but what it will have to do is also from the main page div swap the background and also i think, swap the width of another div. I've got it swapping the background and removing the things I don't want (although they slide down rather then to the left)
This video shows what happens (and then at the end i use firebug to show you what i want to happen) http://www.youtube.com/watch?v=tti_L_ofelg&feature=youtu.be (edit: video online now)
Here is my html for the sliding div:
The CSS:
.slidingDiv {
}
.show_hide {
display:none;
}
And the jQuery:
var defOpen = 1;
jQuery(".slidingDiv").show();
jQuery(".show_hide").show();
jQuery('.show_hide').click(function(){
if(defOpen == 1)
{
jQuery(".show_hide").show();
jQuery("#bgwrap").css("background","url(assets/stripeclear.png) fixed 0 0 repeat-y")
jQuery("#primary_right").css("width","")
jQuery(".slidingDiv").slideToggle();
defOpen = 0
} else {
jQuery(".show_hide").show();
jQuery("#bgwrap").css("background","url(assets/stripe.png) fixed 0 0 repeat-y")
jQuery(".slidingDiv").slideToggle();
defOpen = 1
}
So it's bgwrap that has the image in it that i have to swap to a clear one (to stop it being there as it's a fixed image on the left)
and primary_right seems to be the one when I remove the width goes full screen (what i'm really trying to acheive)
It also needs to be able to toggle closed and open!
Thanks for any help you can give!
question is not clear..but as far as i understood...you want to hide a left side division by which the right side division occupies full screen and vice versa..is it?????
if yes defOpen variable will not help u..use JQuery toggle function...datz the best way to do it...
I think you need to set the width to "inherit". That's essentially what you're doing when you are selecting the delete style in Firebug.
jQuery("#primary_right").css("width","inherit")