How do I reset the slider to slide 1 onLeave? - javascript

I'm using the latest version (2.1.8) of fullPage.js in order to create a dashboard, which automatically slides vertically and horizontally by updating the window.location.
See jsfiddle.
The issue I'm facing is that when we come back around to section 2, the slider is still on the final slide position and has to slide back to slide 1, before progressing forward through the slides.
I've tried following the instructions of Issue #129 ,which looks to have worked for someone using version 1.7.2:
Make scrollSlider function public
Change the name of the calls to the function
Apply onLeave event code:
onLeave: function(index, nextIndex, direction){
//getting the section by the index
var section = $('.section').eq((index - 1));
//if the section we are leaving has slides...
if (section.find('.slides').length) {
//moving to slide 0
$.fn.fullpage.scrollSlider(section, 0);
}
}
However, this has no affect on the behaviour.
Is anybody able to suggest a method to ensure the slider is always put back to the default slide when moving to the next section using the latest version of fullPage.js?
All the best,
AshBestos

I had the same problem. Solved with a copy from function scrollSlider().
Named it function scrollDefault() and set this function public.
In my jquery.fullPage.custom.js
$.fn.fullpage.scrollDefault = function(section, slide){
if(typeof slide != 'undefined'){
var slides = section.find('.slides');
var destiny = slides.find('[data-anchor="'+slide+'"]');
if(!destiny.length){
destiny = slides.find('.slide').eq(slide);
}
if(destiny.length){
landscapeScroll(slides, destiny);
}
}
};
The setTimeout is a quick & dirty solution to hide the scroll animation. So when you click on another section and go back you see the first slide.
In my $(document).ready(function() {
onLeave: function(index, nextIndex, direction){
setTimeout(function(){
var section = $('.section').eq((index - 1));
if (section.find('.slides').length) {
$.fn.fullpage.scrollDefault(section, 0);
}
}, 500);
}

Related

Letter by letter animation with delay in loading

I'm trying to implement letter by letter animation on section with two slides. Currently I'm using jQuery code for that, but I'm afraid it's far from the ideal option.
Here's my code example: codepen
$.fn.animation = function () {
//get the welcome msg element
var $message = $(this);
//get a list of letters from the welcome text
var $getList = $(this).text().split("");
//clear the welcome text msg
$(this).text("");
//loop through the letters in the list array
$.each($getList, function(idx, element) {
//create a span for the letter and set opacity to 0
var newElement = $("<span/>").text(element).css({
opacity: 0
});
//append it to the welcome message
newElement.appendTo($message);
//set the delay on the animation for this element
newElement.delay(idx * 90);
//animate the opacity back to full 1
newElement.animate({
opacity: 1
}, 1100);
});
};
$('.introduction').animation();
$('.secondary').animation();
First problem is that I can't do, so the second sentence with class "secondary" runs only after first one is finished. I've tried to use .delay and setTimeout, but that doesn't help.
Also I'm not sure, how to start animation on second slide, when it's loaded.
I know there's plugins for that stuff, but I'd like to do that in vanilla JavaScript, or jQuery, css3.
Would be glad for any help.
And yeah, here's an example how I would like it to look - http://bootstrapart.net/influence/v1.5.3/
Is it possible to make, so the animation starts every time, when slide is changed?
Thanks.
Edited
Click here to see the whole code working.
Changes I made in css: I set the opacity of html text tags (<h1>,<h3> and <h4>) to 0, so they are hidden. Then in the animation function they are made visible again.
Changes I made in script: To start the second text animation with delay I used setTimeout() function:
setTimeout(function(){
$('.secondary').animation();
},2000);
To detect the slide event of carousel, according to Bootstrap Documentation you can use this method:
$('.carousel').on('slid.bs.carousel', function () {
// here is where you start the animation for the second slide
})
Re-Edit
To track on which slide we are I introduced a variable caled: var $wichSlide. Here is the working method to start the animation when slide is changed:
$('.carousel').bind('slid.bs.carousel', function (e) {
if($whichSlide == 1){
//set $whichSlide position for the next slide event
$whichSlide = 2
//start to animate the text
$('.introduction').animation();
setTimeout(function(){
$('.secondary').animation();
},2000);
/*set the text on the second slide to be invisible
* because we don't want it to appear before animation starts
*/
$('.slide2').css("opacity",0);
}else if($whichSlide == 2){
$whichSlide = 1;
$(".carousel").carousel("pause");
$(".slide2").animation();
setTimeout(function(){
$(".carousel").carousel();
},3000);
$('.introduction').css("opacity",0);
$('.secondary').css("opacity",0);
}
});
See the working code

Change slide in carousel depending on URL

I'm currently using JQ Carousel (http://5509.github.io/jq.carousel/) and I'm trying to figure out how to change the slide in the carousel after the load pages. For example, there are 3 slides. When I click on the 3rd slide and loads a page with a URL containing ".../room-3/...", the slide on the loaded page will show the 3rd slide instead of showing the initial slide.
I tried the script below but no success. Any help is appreciated!
Update: Below is the entire script that I use for the carousel. This appears on all the pages that has the carousel. That last portion of the script, I'm assuming, needs to be updated so it would work on the other pages.
<script src="/path/to/jq.carousel.js"></script>
<script>
// Script that appears on all pages
(function() {
var $carousel = $('#carousel_2').carousel({
start: 1,
indicator: true
});
var currSlide = 1;
var totalSlides = $(".carousel_box").length - 4;
$('#carousel_2_prev').on('click', function(ev) {
ev.preventDefault();
$carousel.carousel('prev');
});
$('#carousel_2_next').on('click', function(ev) {
ev.preventDefault();
$carousel.carousel('next');
});
}());
// Script that appears only on the third page that has the third room
$(document).ready(function() {
if(window.location.href.indexOf('/room-3/') >= 0) {
var $carousel = $('#carousel_2').carousel({
start: 3
});
});
});
</script>

Make last image in slider pause before reseting loop (slider from scratch)

I'm working on a slider from scratch (trying to get the hang of it) but I've come up with a problem. In my slider all slides take 3 seconds before going to the next one, but once the last slide has been reached, no time to view it is given.
I want to add a smooth transition from the last slide to the first.
Here's a fiddle http://jsfiddle.net/4wnrwkf0/
function startSlider() {
interval = setInterval(function() {
$sliderContainer.animate({'margin-left': '-=+100%'}, animationSpeed, function() {
if (++currentSlide === $slides.length+1) {
currentSlide = 1;
$sliderContainer.css('margin-left', 0);
}
});
}, pause);
}
I'm guessing my problem is somewhere around this code
For the time being I'm using a blank slide at the end...but I don't want to leave it like that :P
Thanks in advance
EDIT:
I found a working solution by repeating the first slide at the end of the ul, seems a bit hackish so if anyone has an idea, i'm willing to try alternatives. Anyways...in case anyone finds it useful: http://jsfiddle.net/eclipticald/2gLh5ks0/
See this fiddle if the effect resolved your problem -- http://jsfiddle.net/4wnrwkf0/5/
FROM:
if (++currentSlide === $slides.length+1) {
currentSlide = 1;
$sliderContainer.css('margin-left', 0);
}
TO:
if (++currentSlide === $slides.length+1) {
$sliderContainer.animate({'margin-left': '0'}, animationSpeed, function() {
currentSlide = 1;
});
}
EDIT. Another updated fiddle, so the last slide won't show the white/blank slide, instead goes to slide #1. http://jsfiddle.net/4wnrwkf0/6/
Cheers.

How can I set my browser window's scrollbar or a div scrollbar to scroll in increments using animate and scrollTop?

The general idea to the site i am designing is to scroll through a set of menu items horizontally and incrementally underneath a static div that will magnify(increase dimensions and pt size) the contents of a menu items. I don't really need help with the magnify portion because i think it's as simple as adding a mag class to any of the menuItem divs that go underneath the static div. I have been messing with this for a few weeks and the code I have for incrementally scrolling, so far, is this:
$(document).ready(function () {
currentScrollPos = $('#scrollableDiv').scrollTop(120); //sets default scroll pos
/*The incrementScroll function is passed arguments currentScrollPos and UserScroll which are variables that i have initiated earlier in the program, and then initiates a for loop.
-The first statement sets up the variables: nextScrollPos as equal to the currentScrollPos(which by default is 120px) plus 240px(the distance to next menuItem), prevScrollPos as equal to the currentScrollPos(which by default is 120px) minus 240px(the distance to next menuItem).
-The second Statement checks to see if the user has scrolled using var userScroll
-The third statement sets: var CurrentScroll equal to the new scroll position and var userScroll to false*/
function incrementScroll(currentScrollPos, userScroll) {
for (var nextScrollPos = parseInt(currentScrollPos + 240, 10),
prevScrollPos = parseInt(currentScrollPos - 240, 10); //end first statement
userScroll == 'true'; console.log('dude'), //end second statement and begining of third
currentScrollPos = scrollTop(), userScroll = 'false') {
if (scrollTop() < currentScrollPos) {
$('#scrollableDiv').animate({
scrollTop: (parseInt(prevScrollPos, 10))
}, 200);
console.log('scrolln up')
} else if (scrollTop() > currentScrollPos) {
$('#scrollableDiv').animate({
scrollTop: (parseInt(nextScrollPos, 10))
}, 200);
console.log('scrolln down')//fire when
}
}
}
$('#scrollableDiv').scroll(function () {
userScroll = 'true';
_.debounce(incrementScroll, 200); //controls the amount of times the incrementScroll function is called
console.log('straight scrolln')
});
});
I have found a variety of solutions that are nigh close: such as a plugin that snaps to the next or previous div horizontally demo, another solution that also snaps and is based on setTimeout demo, but nothing that nails incrementally scrolling through divs. I also found a way to control the rate at which a user may scroll through the menuItems using debounce which is included in the above code.
The console.logs inside the loop do not fire when I demo the code in jsfiddle which leads me to believe the problem lies within the loop. I'm a noob though so it could be in syntax or anywhere else in the code for that matter. Also in the second demo, i have provided the css for the horizontal static div, but the moment I put it in my html it keeps the js from working.
I would like to write the code instead of using a plugin and any help would be appreciated! Also, thank you ahead of time!
Try this fiddle. Menu container height is 960px to show 4 menu items. "Zoom" div is positioned absolutely at top. When you scroll mouse over this div, menu items shifts to top/bottom. I had to add additional div to bottom to be able to scroll to last 3 menu items. JS code:
jQuery(document).ready(function($){
var current = 0;
var menu = $('.menu-container').scrollTop(0);
var items = menu.find('.menu-item');
var zoom = $('.zoom');
function isVerticalScroll(event){
var e = event.originalEvent;
if (e.axis && e.axis === e.HORIZONTAL_AXIS)
return false;
if (e.wheelDeltaX)
return false;
return true;
}
function handleMouseScroll(event){
if(isVerticalScroll(event)){
var delta = event.originalEvent.wheelDelta * -1 || event.originalEvent.detail;
current += (delta > 0 ? 1 : -1);
if(current < 0)
current = 0;
if(current >= items.length){
current = items.length - 1;
}
menu.stop().animate({
"scrollTop": current * 240
}, 300);
items.removeClass('current').eq(current).addClass('current');
event && event.preventDefault();
return false;
}
}
zoom.on({
"MozMousePixelScroll": handleMouseScroll,
"mousewheel": handleMouseScroll
});
});
Hope it will help.

building a Jquery Tool slider like yahoo.com slider details

Hi im trying to achieve a "news slider" like the one you can see in yahoo.com... I almost have the 100% of the code.. (if you want to compare them here is my code http://jsfiddle.net/PcAwU/1/)
what is missing in my code , (forget about design) is that , In my slider you have to clic on each item, i tried to replace Onclick for Hover on the javascript, it worked, but the fisrt image on the gallery stop working, so when you just open the slider, you see a missing image.
Other point.. also very important, in yahoo.com after "x seconds" the slider goes to the next item, and so on ... all the Thumnails are gruped 4 by for 4, (in mine 5 by 5, thats ok) ... after pass all the 4 items, it go to the next bloc..
HOW CAN I ACHIEVE THAT!!. I really looked into the API, everything, really im lost, i hope someone can help me. cause im really lost in here.
Thanks
Here is the script
$(function() {
var root = $(".scrollable").scrollable({circular: false}).autoscroll({ autoplay: true });
$(".items img").click(function() {
// see if same thumb is being clicked
if ($(this).hasClass("active")) { return; }
// calclulate large image's URL based on the thumbnail URL (flickr specific)
var url = $(this).attr("src").replace("_t", "");
// get handle to element that wraps the image and make it semi-transparent
var wrap = $("#image_wrap").fadeTo("medium", 0.5);
// the large image from www.flickr.com
var img = new Image();
// call this function after it's loaded
img.onload = function() {
// make wrapper fully visible
wrap.fadeTo("fast", 1);
// change the image
wrap.find("img").attr("src", url);
};
// begin loading the image from www.flickr.com
img.src = url;
// activate item
$(".items img").removeClass("active");
$(this).addClass("active");
// when page loads simulate a "click" on the first image
}).filter(":first").click();
// provide scrollable API for the action buttons
window.api = root.data("scrollable");
});
function toggle(el){
if(el.className!="play")
{
el.className="play";
el.src='images/play.png';
api.pause();
}
else if(el.className=="play")
{
el.className="pause";
el.src='images/pause.png';
api.play();
}
return false;
}
To fix the hover problem you need to make some quick changes: Change the click to a on(..) similar to just hover(..) just the new standard.
$(".items img").on("hover",function() {
....
Then You need to update the bottom click event to mouse over to simulate a hover effect. Trigger is a comman function to use to trigger some event.
}).filter(":first").trigger("mouseover");
jSFiddle: http://jsfiddle.net/PcAwU/2/
Now to have a play feature, you need a counter/ and a set interval like so:
var count = 1;
setInterval(function(){
count++; // add to the counter
if($(".items img").eq(count).length != 0){ // eq(.. select by index [0],[1]..
$(".items img").eq(count).trigger("mouseover");
} else count = 0; //reset counter
},1000);
This will go show new images every 1 second (1000 = 1sec), you can change this and manipulate it to your liking.
jSFiddle: http://jsfiddle.net/PcAwU/3/
If you want to hover the active image, you need to do so with the css() or the addClass() functions. But You have done this already, All we have to do is a simple css change:
.scrollable .active {
....
border-bottom:3px solid skyblue;
Here is the new update jSFilde: http://jsfiddle.net/PcAwU/4/

Categories