Today I'm going to take you through a simple jQuery slideshow and by going through the process step by step, hopefully show you just how easy it is to come up with an idea and achieve it with jQuery.
Let's start with the
HTML.
<div
id="slideshow">
<div id="slideshowWindow">
<div class="slide">
<img src="slide1.jpg"
/>
</div><!--/slide-->
<div class="slide">
<img src="slide2.jpg"
/>
</div><!--/slide-->
<div class="slide">
<img src="slide3.jpg"
/>
</div><!--/slide-->
</div><!--/slideshowWindow-->
</div><!--/slideshow-->
Now Lets add
some css:
#slideshow #slideshowWindow {
width:500px;
height:257px;
margin:0;
padding:0;
position:relative;
overflow:hidden;
}
#slideshow #slideshowWindow .slide {
margin:0;
padding:0;
width:500px;
height:257px;
float:left;
position:relative;
}
Now we need to add some jQuery to make our slides move. If you haven't got it already, you need to download jQuery. Once you've got it, you'll need to include the file in the head section of your page, making sure that you've got the path correct:
<script type="text/javascript" src="jquery.js"></script>
Now we can start writing the jQuery code that'll make out slides move.The first thing we need is our document.ready function. In the head, below where you called the jQuery file, add the following:
<script type="text/javascript">
$(document).ready(function() {
//our code will go here
});
</script>
Now we're set up and ready to go. First we're going to need some variables. Type the following in the document.ready function:
var currentPosition = 0;
var slideWidth = 500;
var slides = $('.slide');
var numberOfSlides = slides.length;
The first variable records which slide we're currently viewing. The slideWidth variable is self explanatory. Next we have a variable called slides which lets us refer to our slides in the jQuery code and the last variable gives us the number of slides in our slideshow. As you can see, it automatically calculates the number of slides by counting the number of divs that are of the class slide.To get the slides to line up across the page, we need to add another div. We're going to do this with jQuery. This div will hold all the slides and allow the float:left property to work. Add the following code:
slides.wrapAll('<div id="slidesHolder"></div>')
Now we need to float the slides so they line up side by side.
slides.css({ 'float' : 'left' });
If you remove the overflow:hidden from the slideshowWindow div and run the code now, you'll see what this achieves.Next we want to set the width of #slidesHolder div. This needs to be set to the width of all the slides added together. We can do this easily in jQuery:
$('#slidesHolder').css('width', slideWidth * numberOfSlides);
We're just using jQuery to change the width set in our css. So now we need to move the slides from one to the other. We will require two functions. The first function will determine how far along we are along our sequence of slides, so we know where to go next and so that when we reach the last slide, we know to jump back to the beginning. Here is the function:
function changePosition() {
if(currentPosition == numberOfSlides) {
currentPosition = 1;
} else {
currentPosition++;
}
moveSlide();
}
The first part of the if/else statement deals with when reach the end of the slide show. When we come to the last slide we want to jump back to the beginning, otherwise the currentSlide variable is simply incremented by 1 and then another function moveSlide is called.
function moveSlide() {
$('#slideHolder').animate({'marginLeft' : slideWidth*(-currentPosition)});
}
This is the business end of the jQuery code, this function sets the left margin of the slidesHolder div to the width of the slide multiplied by the slide number and then animates to that from the current left margin. But if you run your code now you'll notice that nothing is happening. That's because we haven't called the changePosition function yet.We want the slide to change every few seconds so we'll set a timer that calls the function periodically.First we need to set a variable to hold the timer. Below the other variables, add the following:
var slideShowInterval;
var speed = 6000;
As you can see we've also added a variable to control the speed. This is the speed in milliseconds, so 6000 is equal to 6 seconds.So now we need to set up our timer:
slideShowInterval = setInterval(changePosition, speed);
We're using the jQuery setInterval function. It takes two parameters, the function that it calls and the speed. So this will call the changePosition function (which in turn calls the moveSlide function) every 6 seconds.Your jQuery code should look like this:
<script type="text/javascript">
$(document).ready(function() {
var currentPosition = 0;
var slideWidth = 500;
var slides = $('.slide');
var numberOfSlides = slides.length;
var slideShowInterval;
var speed = 3000;
slideShowInterval = setInterval(changePosition, speed);
slides.wrapAll('<div id="slidesHolder"></div>')
slides.css({ 'float' : 'left' });
$('#slidesHolder').css('width', slideWidth * numberOfSlides);
function changePosition() {
if(currentPosition == numberOfSlides - 1) {
currentPosition = 0;
} else {
currentPosition++;
}
moveSlide();
}
function moveSlide() {
$('#slidesHolder')
.animate({'marginLeft' : slideWidth*(-currentPosition)});
}
});
</script>
Go Next Lesson for Full Slide Code >>
+ comments + 23 comments
Thank you for the valuable information i got here..
Web Design Company | Web Development Company
nice information
Visit Once | Click here
thank for Jquery it is realy nice information
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Watch Live TV live tv
Helpful information. Thanks.
Best On Internet
Great article, you get get the best things on internet updates, posts, gadgets here.
Thanks for share such an nice post. Vashikaran Specialist Astrologer in UK
Impressive it is. Husband wife vashikaran
That’s a very great article I like to read your all articles I will recommend it to others to visit here thanks from. assignment-editing
best acupuncture treatment in delhi ncr
best acupuncture doctor in delhi ncr
best acupuncture clinic in delhi ncr
best psychological doctor in delhi ncr
best acupuncture doctor in the world
Great slideshow tutorial. We can use this on our website of CBC Business Brokers Sydney. Thanks a lot.
Marvelous work!. Blog is brilliantly written and provides all necessary information I really like this site. Thanks for sharing this useful post.Thanks for the effective information.
https://www.bharattaxi.com
Moontextile.com is the best e-commerce web plateform which provide the best stuff in mens shalwar kameez. They are manufacturer and use the best cotton/wash&wear fabric with excellent stitching to full fill your need for men shalwar kameez online shopping in very reasonable price range.
white salwar kameez , white dress salwar kameez , white mens shalwar kameez
Eye-Catching Web Designing In Lahore
Marketing92 is the best Web development company in Pakistan that responds to your business. We have over 8 years of
experience in designing and developing websites. Our clients
span many different industry sectors. Web Development in Lahore
have successfully delivered hundreds of websites i.e web applications,
e-commerce websites and social media platforms. We Having a fast, well-coded,
accessible web site creates an enjoyable online experience. Our Web Designing In Lahore,
is committed to providing websites that are as engaging as they are eye-catching. Please feel free to take a look at our web portfolio
http://www.marketing92.com/web-development/
It seems very attractive and informative post. The big thing is it helps us lot. Keep sharing with us.
Web Design San Antonio Texas
Website design SanAntonio
The dress you have put on is really attractive. It does suit on you. Thank You.
Best money making apps 2019
Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.i also want to share about One of the best sap hana training videos . expecting more articles from you.
Welcome to our Pitbull breeder home were we have been proudly breeding and showing quality AKC registered Pitbull puppy since 2004. Our main focus is to produce show quality pitbull puppies with amazing personalities suitable for their forever family. We treat every Puppy with individualized care based on their needs to give them the best opportunities in life. Click "Available Pitbull puppies" to check out our puppies and dogs looking for new homes.
The appearance of this cat's hairlessness is its most distinguishing feature. The Sphynx has a medium-sized body and a surprising amount of weight for its size. The skin texture is similar to that of a soft peach or a smooth nectarine, and the body is warm and soft to the touch. The Sphynx has a pleasant temperament, is lively, and is easy to handle.
https://downtownbreeder.company.com
sphynx kittens for sale massachusetts
sphynx breeders washington
sphynx kittens for sale california
sphynx kittens for sale washington
sphynx kittens for sale in ny
sphynx cat oklahoma
sphynx kittens for sale near me
sphynx kittens for sale
sphynx kittens washington state
sphynx kittens portland oregon
sphynx kittens for sale washington state
sphynx kittens seattle wa
sphynx kittens near me
sphynx kitten for sale seattle
sphynx kitten price
sphynx kittens
sphynx kittens for sale in va
sphynx kittens for sale oklahoma
Informative and useful. wanted to know what is the next national day and how it is celebrated? You can know about all from https://thenextnationalday.com/ and its celebrations. Thank you!!
Click here for buy 100% original supplements
Nero Platinum Crack
Nero Platinum Crack is when it comes to copying CDs, DVDs, and Blu section discs. Clean and splendid ripping, duplication, automatic backup and, copy approaches, backup reports on CD, DVD, and Blu section discs. After that, Adapt to watch DVD movies with create 3D menus, copy, duplicate, propose, move and mix music like a DJ. Convert music, photos, and DVDs for playback on your iPod and various phones. Quickly move images and videos to My Nero, YouTube, and MySpace, watch, record, delay, and edit your live TV experience, Blu section playback, AVCHD, and other high definition social events.
https://zhlicense.com/
The Digital Imprint Office is the best suited office lens for professionals. The Digital Office Lens provides anti-fatigue and clear intermediate vision. It has all the benefits of a Digital Free-Form back surfaced lens technology. The dynamic software allows office lenses to be designed in more variations. The design is that of a relax lenses and allows enhanced reading and intermediate vision while being effective.
Based on the users’ requirements, any desired regression (dynamic) from (0.50 up to 2.75) would allow a distance view of 0.80 up to 4.00 meters. The following 4 regressive powers, -0.75, -1.25, -1.75 and -2.25, would allow the optician to provide the correct intermediate vision required for the patient, along with excellent reading properties.
Benefits:
The lens is perfect for users who require high, additional powers (3.50 D)
The lens ensures a smooth transition from Near to Intermediate vision
Digital surface accuracy provides easy adaptation and reduces eyestrain
Clear and sharper vision for computer user and office work .
for more https://www.gkboptic.com/products/occupational-lenses/digital-office/
Post a Comment
Thank you for your valuable comment...