How to move slides in javascript in a webpage - javascript

I have the following code in javascript to move my slides:
var slider = {
el: {
slider: $("#slider"),
allSlides: $(".slide"),
sliderNav: $(".slider-nav"),
allNavButtons: $(".slider-nav > a")
},
timing: 300,
slideWidth: 100%
init: function() {
this.bindUIEvents();
},
bindUIEvents: function() {
this.el.slider.on("scroll", function(event) {
slider.moveSlidePosition(event);
});
this.el.sliderNav.on("click", "a", function(event) {
slider.handleNavClick(event, this);
});
},
moveSlidePosition: function(event) {
// Magic Numbers =(
this.el.allSlides.css({
"background-position": $(event.target).scrollLeft()/6-100+ "px 0"
});
},
handleNavClick: function(event, el) {
event.preventDefault();
var position = $(el).attr("href").split("-").pop();
this.el.slider.animate({
scrollLeft: position * this.slideWidth
}, this.timing);
this.changeActiveNav(el);
},
changeActiveNav: function(el) {
this.el.allNavButtons.removeClass("active");
$(el).addClass("active");
}
};
slider.init();
I get no error in the console regarding this but the slides should move after every 3 seconds automatically, they are not doing it now. Does anyone know whats the problem here. Any suggestion would be highly appreciated.

You have a syntax issue with a percent it must be a string and have the comma following to not break your object.
Should this line slideWidth: 100% be slideWidth: "100%" ,

Related

Creating an iframe from JQuery - properties are not working, why not?

Hello im creating an iframe in my javascript. However the style and the loaded event doesnt seem to work. What am I doing wrong?
var iframe = $("<iframe src='/Player/ShowPage/"+data.PageId+"'/>", {
style: "position:absolute; left:-100%",
load: function () {
var that = this;
$(this).animate({
left: 0
}, 500);
$('iframe').not(this).animate({
left: '-100%'
}, 500, function () {
$('iframe').not(that).remove();
});
}
});
$('body').append(iframe);
I must be missing something, I just cant figure out what it is. The iframe is just sitting there - mocking me - on the screen...
You just need to move the 'src' into the object literal and everything works fine.
var iframe = $("<iframe>", {
src: '/Player/ShowPage/' + data.PageId,
style: "position:absolute; left:-100%",
load: function () {
var that = this;
$(this).animate({
left: 0
}, 500);
$('iframe').not(this).animate({
left: '-100%'
}, 500, function () {
$('iframe').not(that).remove();
});
}
});
$('body').append(iframe);
Please check this JSFIDDLE for a working demo.

Javascript load on page

I want that the title and subtitle shows up when the page loads. I tried several times to let it work but it won't.
jQuery(document).ready(function(){
jQuery('.element').bind('mouseover', function() {
jQuery(this).find('img').stop().each(function() {
jQuery(this).animate({
'opacity': 1
}, 200);
});
});
jQuery('.element').bind('mouseout', function() {
jQuery(this).find('img').stop().each(function() {
jQuery(this).animate({
'opacity': 0.4
}, 200);
});
});
jQuery('.element').on('pageload', function() {
jQuery(this).find('.title').stop().each(function() {
jQuery(this).animate({
'margin-left': 35,
'opacity': 1
}, 250);
});
jQuery(thi2s).find('.subtitle').stop().each(function() {
jQuery(this).animate({
'opacity': 1
}, 0);
jQuery(this).delay(150).animate({
'margin-left': 35
}, 250);
});
});
});
Can someone help me with this problem?
It's not entirely clear what you are trying to do, but at a first glance, I noticed this bug:
jQuery(thi2s).find('.subtitle').stop().each(function() {
// code
}
Obviously, the extraneous '2' should not be there, but I'm not sure if this small fix will create the intended behavior you are looking for.
Here's an example of how you should structure your code. You shouldn't really use the .bind() function for your events or reference elements using jQuery('.element'). Additionally the excessive use of this is unnecessary and complicates your code.
Consider this basic example:
$(document).ready(function () {
$("div").hover(
function () {
$("h2").animate({
left: 50,
opacity: 1
}, 500);
}, function () {
$("h2").animate({
left: 50,
opacity: 0.2
}, 500);
});
});
Check out this JSFiddle.
Hope this helps!

jQuery event for a slider

I want to create a button with a event which occurs when a user clicked on it.
If someone has clicked this button a slider shows up.
I want a function if no one release this slider div the div toggles after 5 seconds, but if someone releases the slider nothing must happen!
I have created a fiddle in hope someone can fix this?
http://jsfiddle.net/84nVQ/50/
function handler1() {
clearTimeout(time);
$('#volumeChange').attr("class", "selected");
$('#volume-slider').slider({
orientation: 'horizontal',
value: 100,
max: 1,
step: 0.01,
animate: true,
slide: function(e, ui) {
alert(ui.value);
}
}).show();
}
function handler2() {
time = setTimeout(function() {
$('#volumeChange').attr("class", "");
$('#volume-slider').toggle();
$('#volume-slider').clearTimeout(time);
}, 5000);
}
function handler3() {
clearTimeout(time);
}
$("#volumeChange").on({
click: function(e) {
handler1();
$('#volume-slider').mouseleave(handler2).mouseenter(handler3);
}
});
There were two errors: undefined time variable and clearTimeout. This code should work
var time = null;
function handler1() {
clearTimeout(time);
$('#volumeChange').attr("class", "selected");
$('#volume-slider').slider({
orientation: 'horizontal',
value: 100,
max: 1,
step: 0.01,
animate: true,
slide: function(e, ui) {
movie.volume = ui.value;
}
}).show();
}
function handler2() {
time = setTimeout(function() {
$('#volumeChange').attr("class", "");
$('#volume-slider').toggle();
clearTimeout(time);
}, 5000);
}
function handler3() {
clearTimeout(time);
}
$("#volumeChange").on({
click: function(e) {
handler1();
$('#volume-slider').mouseleave(handler2).mouseenter(handler3);
}
});

How would I control a JCarousel inside another JCarousel?

Using JCarousel I currently have a carousel inside another carousel, which I can trigger to start playing slides by clicking on a play button that I created on the outside carousel. These are all loaded dynamically.
I'd like to have the inner carousel stop animating and go back to it's original loaded position when I click to change the slide of the outer carousel.
Here is what I have currently for manipulating JCarousel:
//outer carousel
$('#carousel').jcarousel({
auto: 0,
wrap: 'circular',
scroll: 1,
initCallback: carousel_initCallBack,
itemVisibleInCallback: {
onBeforeAnimation: gallerycarousel_itemfirst
}
// Info Box
$('.info').click(function() {
var itemid = $(this).attr('id').substring(5);
$.ajax({
url: 'php/get_case_studies.php?item=' + itemid,
dataType: 'json',
success: function(data) {
var infobox = $('#box_info' + data.data[0].id)
infobox.html('');
var content = '<div class="main_sec">'+data.data[0].main_desc+'</div><div class="sec_sec">'+data.data[0].sec_desc+'</div><div class="visit"><span class="box_num"><img src="img/visitor.png" alt="#" /> Visitors:</span>'+data.data[0].visitors+'</div><div class="avg"><span class="box_num"><img src="img/time.png" alt="#"/> Average time on site:</span>'+ data.data[0].avg_time +'</div>';
infobox.append(content);
$('.box_info').animate({left: '0px'}, 200);
}
});
});
function carousel_initCallBack(carousel) {
carousel.clip.hover(function() {
$('.gallery_control').stop().animate({"top": "454px"}, 200);
carousel.stopAuto();
}, function() {
$('.gallery_control').stop().animate({"top": "524px"}, 200);
$('.box_info').animate({left: '-302px'}, 200 );
carousel.startAuto();
});
$('#gallerycarousel_controls .btn_right').click(function() {
carousel.next();
return false;
});
$('#gallerycarousel_controls .btn_left').click(function() {
carousel.prev();
return false;
});
$('.grid').click(function() {
$('.gallery_control').animate({"top": "524px"}, 200);
$('#grid_view').fadeIn('slow');
carousel.stopAuto();
$('#grid_view').mouseleave(function() {
$(this).fadeOut('slow');
carousel.startAuto();
});
});
$('#carousel_slides .btn_item').click(function() {
carousel.scroll($.jcarousel.intval($(this).attr('id')));
$('#grid_view').fadeOut('slow');
$('.gallery_control').animate({"top": "454px"}, 200);
return false;
});
//this is the play button which generates and starts the inner carousel
$('.play').click(function() {
var itemid = $(this).attr('id').substring(5);
$('#int_carousel' + itemid).load('get_slideshow.php?slideshow&item=' + itemid, function() {
$('#int_carousel' + itemid).jcarousel({
auto: 3,
wrap: 'circular',
scroll: 1
});
});
});
}
I have tried adding a click function within the 'carousel_initCallBack' function to see if I can control the inner carousel like this:
$('#gallerycarousel_controls .btn_right').click(function() {
$('#int_carousel' + itemid).stopAuto();
});
The above doesn't work as I have tried and this would probably not bring the inner carousel back to the original position of the slide.
I have went on multiple different forums and have been searching everywhere I could on the internet and I am still not able to find any solid help or assistance. I am in dire need of assistance on this as I ran out of options. Please if someone can help me, I'd appreciate it so much. I am still new to web development and need guidance.

jCarousel, Counter, how?

Im using this plugin: http://sorgalla.com/projects/jcarousel/
I want there do be a counter that can display:
1 of 4
When clicking the next button, it should say:
2 of 4 and so on...
The script is the default initialization:
<script type="text/javascript">
jQuery('#mycarousel').jcarousel();
</script>
However, haven't seen an example of this in the documentation ? So any help would be appreciated...
Thx
Uses something like this:
$(document).ready(function () {
var nmrElements = $('#mycarousel').children('li').length;
$('#mycarousel').jcarousel({
scroll: 1,
itemLoadCallback: function (instance, controlElement) {
if (instance.first != undefined) {
$('#label').html(instance.first + ' of ' + nmrElements);
}
}
});
});
Should be kind of impossible to realize with what is given there. Since it's just a ul that is hidden behind a div and only that ul element gets scrolled when you click on a button.
There is no semantic property that you could use to identify the currently visible object.
jQuery(document).ready(
function() {
function mycarousel_initCallback(carousel) {
// alert(this.mycarousel);
// alert("inside carousel");
// Disable autoscrolling if
// the user clicks the prev
// or next button.
carousel.buttonNext.bind('click', function() {
carousel.startAuto(0);
});
carousel.buttonPrev.bind('click', function() {
carousel.startAuto(0);
});
// Pause autoscrolling if
// the user moves with the
// cursor over the clip.
carousel.clip.hover(
function() {
carousel.stopAuto();
},
function() {
carousel.startAuto();
});
}
jQuery('#mycarousel').jcarousel({
auto : 2,
scroll : 1,
wrap : 'last',
initCallback : mycarousel_initCallback,
itemFallbackDimension: 300,
//size: mycarousel_itemList.length,
itemFirstInCallback:mycarousel_itemFirstInCallback
// itemLoadCallback: { onBeforeAnimation: mycarousel_itemLoadCallback}
});
});
$(".jcarousel-prev").after("<div><h6 id=\"myHeader\" style=\"color:#FFFFFF;width:68%; top:85%; left:40px;font-size:100%; display: block;\" class=\"counterL\"></h6></div>");
function display(s) {
$('#myHeader').html(s);
};
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
display( idx +"<i> of </i>"+ $("#mycarousel li").length);
};
You can assign id-s to each image in carousel. And add some javascript to write the number from id of that images.
And also:
<script type="text/javascript">
function Callback_function(elem) {
document.write(this.id);
}
jQuery('#mycarousel').jcarousel({
scroll: 1,
initCallback: Callback_function,
});
</script>
it is simple, just open the following file:
sites\all\modules\jcarousel\js\jcarousel.js
and in line 146:
carousel.pageCount = Math.ceil(itemCount / carousel.pageSize);
change it to this: carousel.pageCount = Math.ceil(itemCount / 1);
it will work superb :D

Categories

Resources