Disable 'Previous' button on 1st slide? Nivoslider - javascript

I'm using Nivoslider for a web project I am working on.
As you can see from the above link, the "prev" button displays even when on the 1st slide.
Is it possible I can make NivoSlider "linear" and only display the previous button if the slide number is greater than 0?
It seems NivoSlider is set up as 'infinite' or 'circular' by default, but I'm sure there must be a way to alter this.
Many thanks for any help with this :-)

You could use a custom afterChange function when you initialize the slider.
var sliderSelector = '#slider'; // Replace with your slider selector
$(sliderSelector).nivoSlider({
// .. Other config here
controlNav: true, // This solution relies on this to work
afterChange: function() {
// Hide prev button if on first slide
if ($(sliderSelector + ' .nivo-controlNav .nivo-control:first').hasClass('active'))
$(sliderSelector + ' .nivo-prevNav').hide();
else
$(sliderSelector + ' .nivo-prevNav').show();
// Hide next button if on last slide
if ($(sliderSelector + ' .nivo-controlNav .nivo-control:last').hasClass('active'))
$(sliderSelector + ' .nivo-nextNav').hide();
else
$(sliderSelector + ' .nivo-nextNav').show();
},
afterLoad: function() {
// If you have randomStart set to true, then you might check with what slide it started and act accordingly.
// If you use the default, then this should be all you need
$(sliderSelector + ' .nivo-prevNav').hide();
}
});

Related

Adding transition to delayed dynamic elements?

I'm trying to add a 'slide in to the left' animation to the class .bot-dialog. This class is dynamically generated and my setTimeout function does not seem to be doing justice.
One of the issues I noticed was that it doesn't work ONLY when the typing indicator is active. Here is my code:
Generate my bot dialog
$("#conversation").html(
"<div class='bot-log'>" +
"<span class='bot'>Chatbot: </span>" +
"<span class='current_msg bot-dialog dialog' style='left:-40px; position:relative;'>TEST" +
"</span> </div>"
)
var $to = $(".bot-dialog");
setTimeout(function() {
$to.css("left", 200 + "px");
}, 0);
$(".current_msg").hide();
//Add and remove typing indicator when firing typing function
$(".bot-log:last-child").append(
'<div class="typing-indicator"><span></span><span></span><span></span></div>'
);
$(".typing-indicator").delay(800).fadeOut(function() {
$(".typing-indicator").remove();
});
$(".current_msg").delay(1200).fadeIn(function() {
$(this).removeClass("current_msg");
if (typeof callback == "function") callback();
});
CSS for dialog/transition
.bot-dialog{
transition:5s all ease;
}
Here is a jsfiddle showing the issue.
Thanks for the help!
The thing is, you're hiding it, which sets it to display: none which affects the css transition. Just use opacity instead.
I've changed the transition from all to left so that the test appears instantly, if you want a smooth opacity, change it back to all
https://jsfiddle.net/Lpdx2a0x/4/
Just change the settimeout from 0 value greater than 0 like for example I have used 1 millisecond.
Change:
var $to = $(".bot-dialog");
setTimeout(function() {
$to.css("left", 200 + "px");
}, 0);
To:
var $to = $(".bot-dialog");
setTimeout(function() {
$to.css("left", 200 + "px");
}, 1);
And now it works fine.
EDIT : I am not sure exactly what animation you are looking for but when I remove $(".current_msg").hide(); and increase the delay it works as per you want.
FIDDLE

click function disabling when another click function is clicked more than once

Just messing around with making a new tab page for chrome just for learning purposes. What it is doing is each time the #refresh div is clicked the background changes, then when #pin is clicked it will pin the background so it doesn't change when the page is loaded.
I have an issue where my #pin div does not call the the click function if the #refresh is clicked twice.
So what is happening currently is that if I select #pin the icon changes to a filled in heart icon (as opposed to the unpinned empty heart icon). Then when refresh is clicked it will refresh the background and change the icon back to the unpinned icon. (empty heart).
This is working though when I click #refresh while the #pin has class "glyphicon glyphicon-heart-empty" already then clicking #pin again does nothing
$("#pin").click(function() {
var storedbg = document.getElementById('background').style.backgroundImage;
if($(this).hasClass("glyphicon glyphicon-heart-empty")){
$(this).removeClass("glyphicon glyphicon-heart-empty");
$(this).addClass("glyphicon glyphicon-heart");
chrome.storage.sync.set({'background' : storedbg});
}
else if ($(this).hasClass("glyphicon glyphicon-heart")){
$(this).removeClass("glyphicon glyphicon-heart");
$(this).addClass("glyphicon glyphicon-heart-empty");
chrome.storage.sync.clear();
}
});
$("#refresh").click(function() {
$('body').css({'background-image': 'url(' + background[Math.floor((Math.random() * 26) + 0)] + ')'});
chrome.storage.sync.clear();
$('#pin').removeClass("glyphicon glyphicon-heart");
$('#pin').addClass("glyphicon glyphicon-heart-empty");
});
Seems the way I wrote the removal of classes was the issue!
Managed to fix with:
$("#refresh").click(function() {
$('body').css({'background-image': 'url(' + background[Math.floor((Math.random() * 26) + 0)] + ')'}); //sets random background*/
if ($('#pin').hasClass("glyphicon glyphicon-heart")){
$('#pin').removeClass("glyphicon glyphicon-heart");
$('#pin').addClass("glyphicon glyphicon-heart-empty");
chrome.storage.sync.clear();
}
});

Using a jquery slider for text instead of images?

This may be a little too specific, but I have a jquery slider that I am using <p> classes instead of images to cycle through customer quotes. Basically the problem I am running into right now is when it is static and non moving (JS code is commeneted out) they are aligned how I want them to be. As soon as the JS is un commented, they stretch out of view and you just see a white box?
Any ideas?
How I want each panel to look like:
jsfiddle
So I sort of made this my Friday project. I've changed a whole lot of your code, and added a vertical-align to the quotes and authors.
Here's the fiddle http://jsfiddle.net/qLca2fz4/49/
I added a whole lot of variables to the top of the script so you could less typing throughout.
$(document).ready(function () {
//rotation speed and timer
var speed = 5000;
var run = setInterval(rotate, speed);
var slides = $('.slide');
var container = $('#slides ul');
var elm = container.find(':first-child').prop("tagName");
var item_width = container.width();
var previous = 'prev'; //id of previous button
var next = 'next'; //id of next button
Since you used a % based width I'm setting the pixel widths of the elements in case the screen is reszed
slides.width(item_width); //set the slides to the correct pixel width
container.parent().width(item_width);
container.width(slides.length * item_width); //set the slides container to the correct total width
As you had, I'm rearranging the slides in the event the back button is pressed
container.find(elm + ':first').before(container.find(elm + ':last'));
resetSlides();
I combined the prev and next click events into a single function. It checks for the ID of the element targeted in the click event, then runs the proper previous or next functions. If you reset the setInterval after the click event your browser has trouble stopping it on hover.
//if user clicked on prev button
$('#buttons a').click(function (e) {
//slide the item
if (container.is(':animated')) {
return false;
}
if (e.target.id == previous) {
container.stop().animate({
'left': 0
}, 1500, function () {
container.find(elm + ':first').before(container.find(elm + ':last'));
resetSlides();
});
}
if (e.target.id == next) {
container.stop().animate({
'left': item_width * -2
}, 1500, function () {
container.find(elm + ':last').after(container.find(elm + ':first'));
resetSlides();
});
}
//cancel the link behavior
return false;
});
I've found mouseenter and mouseleave to be a little more reliable than hover.
//if mouse hover, pause the auto rotation, otherwise rotate it
container.parent().mouseenter(function () {
clearInterval(run);
}).mouseleave(function () {
run = setInterval(rotate, speed);
});
I broke this in to its own function because it gets called in a number of different places.
function resetSlides() {
//and adjust the container so current is in the frame
container.css({
'left': -1 * item_width
});
}
});
//a simple function to click next link
//a timer will call this function, and the rotation will begin :)
And here's your rotation timer.
function rotate() {
$('#next').click();
}
It took me a little bit, but I think I figured out a few things.
http://jsfiddle.net/qLca2fz4/28/
First off, your console was throwing a few errors: first, that rotate wasn't defined and that an arrow gif didn't exist. Arrow gif was probably something you have stored locally, but I changed the 'rotate' error by changing the strings in the code here to your actual variables.
So, from:
run = setInterval('rotate()', speed);
We get:
run = setInterval(rotate, speed);
(No () based on the examples here: http://www.w3schools.com/jsref/met_win_setinterval.asp)
But I think a more important question is why your text wasn't showing up at all. It's because of the logic found here:
$('#slides ul').css({'left' : left_value});
You even say that this is setting the default placement for the code. But it isn't..."left_vaule" is the amount that you've calculated to push left during a slide. So if you inspect the element, you can see how the whole UL is basically shifted one slide's worth too far left, unable to be seen. So we get rid of 'left_value', and replace it with 0.
$('#slides ul').css({'left' : 0});
Now, there's nothing really handling how the pictures slide in, so that part's still rough, but this should be enough to start on.
Let me know if I misunderstood anything, or if you have any questions.
So, a few things:
1) I believe you are trying to get all of the lis to be side-by-side, not arranged up and down. There are a few ways to do this. I'd just make the ul have a width of 300%, and then make the lis each take up a third of that:
#slides ul {
....
width: 300%;
}
#slides li {
width: calc(100% / 3);
height:250px;
float:left;
}
2) You got this right, but JSFiddle automatically wraps all your JS inside a $(document).ready() handler, and your function, rotate needs to be outside, in the normal DOM. Just change that JSFiddle setting from 'onload' to 'no wrap - in head'
3) Grabbing the CSS value of an element doesn't always work, especially when you're dealing with animating elements. You already know the width of the li elements with your item_width variable. I'd just use that and change your code:
var left_indent = parseInt($('#slides ul').css('left')) - item_width;
$('#slides ul').animate({'left' : left_indent}, 1500, function () {
to:
$('#slides ul').stop().animate({'left' : -item_width * 2}, 1500, function () {
4) Throw in the .stop() as seen in the above line. This prevents your animations from overlapping. An alternative, and perhaps cleaner way to do this, would be to simply return false at the beginning of your 'next' and 'prev' functions if #slides ul is being animated, like so:
if ($('#slides ul').is(':animated')) return false;
And I think that's everything. Here's the JSFiddle. Cheers!
EDIT:
Oh, and you may also want to clearInterval at the beginning of the next and prev functions and then reset it in the animation callback functions:
$('#prev').click(function() {
if ($('#slides ul').is(':animated')) return false;
clearInterval(run);
$('#slides ul').stop().animate({'left' : 0}, 1500,function(){
....
run = setInterval('rotate()', speed);
});
});

change id of button depending on current viewed div

ive a problem which is driving me crazy. im trying to explain...
i have a very long scrolling page with about 10 divs, one below the other (no space between).
at the bottom of the viewing port is a button with an id and a position: fixed. when i scroll up or down the button is fixed while the divs move up or down.
i want to have different id's on the button depending on which div layer is in the viewing port. that means if one divlayer fills over 50% of the available space the href of the button should change...
i tried the inview.js, but the problem is, that 2 divs at the same time have the inview class...
my current code:
$('#div4, #div5, #div6').bind('inview', function (event, visible) {
if (visible == true) {
$(this).addClass("inview");
} else {
$(this).removeClass("inview");
}
});
var $div4 = $('#div4');
if($div4.hasClass("inview")){
$('#button1').attr('id', 'button2');
}
you see, every div which is in the viewport the button gets a new id.
has anyone of you a solution?
thanks ted
You can try to remove the inview class before adding it.. Something like this:
var $divs = $('#div4,#div5,#div6';
$divs.bind('inview', function (event, visible) {
$divs.not(this).removeClass("inview");
if (visible == true) {
$(this).addClass("inview");
}
});
Another suggestion is to use the Waypoints plugin and fire when the div crosses the 50% mark.
The only difficult part is that depending on the direction you'll need to select the current div or the one above.
Plugin: http://imakewebthings.com/jquery-waypoints/
Demo: http://jsfiddle.net/lucuma/nFfSn/1/
Code:
$('.container div').waypoint(function (direction) {
if (direction=='up')
alert('hit ' + $.waypoints('above')[$.waypoints('above').length-2].id);
else
alert('hit ' + $(this).attr('id'));
}, {
offset: $.waypoints('viewportHeight') / 2
});

jQuery and CSS Menu Help!

I'm attempting to make a menu bar that sticks to the bottom of the screen. Due to it's position on the screen I can't use anchor tags for hyperlinking because in Google Chrome it causes that small link bar to appear in the bottom corner (which overlays ontop of the menu).
As such, each menu icon is a DIV with a unique ID (eg. "profile") and the class "menu-item" is applied to it. Each of these icons will link to a specific page when clicked on (eg. why I want to use the onClick javascript event). However, when each of these icons is hovered over it pops a contextual tooltip (or submenu) above it. Inside this tooltip a further options or links. Consequently, I have come up with the following html construct:
example image located here: http://i.stack.imgur.com/hZU2g.png
Each menu icon will have its own unique onClick link, as well as its own unique submenu/tooltip (which may have more links to different pages).
I am using the following jQuery to pop each submenu:
$(".menu-item").hover(function() {
$('#' + this.id + '-tip').fadeIn("fast").show(); //add 'show()'' for IE
},
function() { //hide tooltip when the mouse moves off of the element
$('#' + this.id + '-tip').hide();
}
);
The issue I'm having is keeping the tooltip visible when the cursor is moved off the icon and onto the submenu/tooltip (currently it disappears the second the icon is no longer hovered on). I want to jQuery fadein and fadeout effects to be applied to the appearance of the tooltip/submenu.
Comments, suggestions, code and jsfiddle examples would be greatly appreciated. Happy to clarify further if I was unclear on any aspects.
Thanks in advance.
You need to wrap the menu-item and tip links in a parent div like so:
<div class="item-wrapper" rel="profile">
<div id="profile" class="menu-item"></div>
<div id="profile-tip" class="tip">
Link1
Link2
</div>
</div>
Then apply the hover function to .item-wrapper and reference the rel attribute (or any other attribute of your choosing):
$(".item-wrapper").hover(function() {
$('#' + $(this).attr("rel") + '-tip').fadeIn("fast").show(); //add 'show()'' for IE
},
function() { //hide tooltip when the mouse moves off of the element
$('#' + $(this).attr("rel") + '-tip').hide();
});
This way when you hover over the links you will still be hovering over the .item-wrapper div.
UPDATE:
To answer your follow-up question, you will need to use setTimeout():
var item_wrapper = {
onHover: function($obj, delay) {
setTimeout(function() {
$('#' + $obj.attr("rel") + '-tip').fadeIn("fast").show(); //add 'show()'' for IE
}, delay);
},
offHover: function($obj, delay) {
setTimeout(function() {
$('#' + $obj.attr("rel") + '-tip').hide();
}, delay);
},
initHover: function($obj, delay) {
$obj.hover(function() {
item_wrapper.onHover($(this), delay);
}, function() {
item_wrapper.offHover($(this), delay);
});
}
};
item_wrapper.initHover($(".item-wrapper"), 1000);
The second argument to setTimeout() is the delay in milliseconds.

Categories

Resources