Creating a straightforward image slideshow using jQuery featuring next and previous buttons

I am looking for assistance in adding next and previous buttons to this slider. I came across a code snippet on a blog that could be useful, which can be found at .net/dk5sy93d/

Answer №1

I'm just starting to explore Jquery and I need to create a basic slider for an upcoming web development competition. Most solutions I've found so far come with a lot of extra code, so I decided to try my hand at creating a simple manual slider control using Jquery.

//Manual Slider Control Jquery

$("#slider-next").click(function(){
if($(".slider-a").is(":visible")){
$(".slider-a").hide(3000);
}else{
$(".slider-b").hide(3000);
}     
});

$("#slider-prev").click(function(){
if(($(".slider-a").not(":visible")) && ($(".slider-b").is(":visible"))){
$(".slider-a").show(3000);
}else if(($(".slider-a").not(":visible")) && ($(".slider-b").not(":visible"))){
$(".slider-b").show(3000);
}
});
/* Slider CSS */
#slider-container{
    margin:auto;
    text-align:center;
}
#slider-content{
    margin:auto;
    height:300px;
    width:400px;
    overflow:hidden;
    border:2px solid #000;
    background-color:grey;
}
#slider-controls{
    margin:auto;
    text-align:center;
    width:400px;
}
#slider-controls button{
    background-color:#000;
    padding:0 15px;
    font-size:1em;
    border-radius: 0 0 15px 15px;
    color:white;
}
#slider-prev{
    float:left;
}
#slider-next{
    float:right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="slider-container">
    <div id="slider-content">
        <img class="slider-img slider-a" src="http://www.light2015.org/.imaging/stk/pop/zoom/dam/CosmicLight/A-Universe-of-Images/Vela_Supernova_Remnant/jcr:content/Vela_Supernova_Remnant.2014-07-17-20-32-16.png" width="400px" height="300px" />
        <img class="slider-img slider-b" src="http://www.sciuridae.co.uk/photography/images/blown_out_skies/two_images.jpg" width="400px" height="300px" />
        <img class="slider-img clider-c" src="http://www1.hw.ac.uk/estate/images/landscape-images/cycleways-and-footpaths-big.JPG" width="400px" height="300px" />
    </div>
    <div id="slider-controls">
        <button id="slider-prev">Previous</button>
        <button id="slider-next">Next</button>
    </div>
</div>

If you have any suggestions on how to simplify this further while maintaining or improving functionality, please leave a comment below!

Answer №2

Check out this JavaScript code snippet for your HTML:

http://jsbin.com/nalewayume/1/edit?js,console,output

function showNextSlide() {
  if(currentIndex < totalSlides) {        
      $slides.eq(currentIndex).fadeOut(200);      
      $slides.eq(currentIndex + 1).fadeIn(200);        
      currentIndex += 1;   
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble with Jquery Library Clash: Overlay Feature Malfunctioning

Having a JQuery conflict is causing two functionalities on my website to not work together. I've attempted to rearrange the script order and change the $ function to the jQuery function, but it doesn't seem to resolve the issue! <div id="ma ...

"Apple TV now features an iOS app that allows users to watch YouTube videos

I am interested in developing an iOS application that can play YouTube videos in a UIWebView window. Users should have the option to play the videos directly on their iOS device or stream them via AirPlay to an AppleTV. The content displayed in the UIWebV ...

The `background-size` property in jQuery is not functioning as expected

I am facing the following issue: When I click on a div with absolute positioning, I want to animate its position, width, and height. In addition, I want to change the background-size using jQuery. However, the problem is that all CSS properties are updat ...

Problem encountered with imaskJS in handling the formatting of data with forward slashes

After conducting tests on the frontend of the imask website at , it has been verified that utilizing a forward slash for date formatting results in the exclusion of the final digit of the date. Various attempts were made, including: IMask( field, ...

What is the best way to create a list using only distinct elements from an array?

If I have a collection of different colors: Red Blue Blue Green I aim to extract only the unique colors and store them in an array. Subsequently, I plan to incorporate each color from that array into an existing color list. The desired outcome would l ...

The retweet option is missing from Twitter Web Intents

I am trying to implement Twitter Web Intents to display modal windows for actions such as "Like" and "Retweet". Following the instructions from https://developer.twitter.com/en/docs/twitter-for-websites/web-intents/overview, my code looks like this: <h ...

Missing transitive dependencies have been identified within the node_modules directory when using npm

In my software development journey, I am working on two npm projects - one called X and the other called Y. Let's say that X relies on angular and has it specified as a dependency in its package.json file. To establish a connection between X and Y, I ...

I need help figuring out how to get a horizontal scrollbar positioned at the top of a div to function properly once the content has been loaded using ajax

HTML CODE <div id="scrollidout" style="overflow: auto; overflow-y: hidden; "> <div id="scrollidin" style="padding-top: 1px; height: 20px; width: 1000px">&nbsp;</div> </div> <div class="resultcontent" style="overflow ...

Creating a customized SelectField component for Material-UI v1.0.0-alpha.21 with a fix for the Menu anchorEl problem

Currently, Material-UI v1.0.0 does not have a selectField implemented yet so I am attempting to create my own version using TextField, Menu, and MenuItem Components. Below is the code for my custom selectField: export default class SelectField extends Rea ...

Effortlessly sending multiple values from text fields using jQuery

i am using jQuery to fetch all values from text fields with the same field name. $('input[name^="StudentName"]').each(function() { StudentName += $(this).val(); alert(StudentName); }); when I alert, all the values are displayed as one s ...

Error importing React Icons with the specific icon FiMoreHorizontal

Currently following a guide to create a Twitter-like application and I need to add the following imports: import { FiMoreHorizontal } from 'react-icons/fi' 2.3K (gzipped: 1K) import { VscTwitter } from 'react-icons/vsc' 3.1K (gzipped: ...

Is there a way to relocate TinyMCE’s toolbar to a modal popup window?

I am currently facing a challenge with TinyMCE and jQuery as I try to relocate TinyMCE's external toolbar within the DOM. Adding to the complexity, there are multiple instances of TinyMCE on the page. My objective is to only move the toolbar for the ...

Extract data using PHP regular expressions

I need to extract a specific portion of HTML data, removing everything above an H1 tag and below an H2 tag with the text 'What we offer'. This extracted data should be stored in a variable. <p>This part is unnecessary</p> <h1>T ...

Anticipate feedback from a new user

I'm currently working on a verification system that involves sending a message in one channel and triggering an embed with two emoji options (accept or deny) in another channel. The issue I'm facing is that the .awaitReaction function is getting ...

The sliding div unites with the bottom button, rather than the top one

When hovering over the upper left button, a div with sample description slides down. However, I am facing two challenges that I cannot seem to resolve. The description is supposed to merge with the second button but instead merges with the first one. I a ...

Angular 2 Aot Issue: CRITICAL ERROR: CALL_AND_RETRY_LAST Allocation unsuccessful - JavaScript heap exhausted

Encountered an issue while running Angular 2 AOT rollup: <--- Last few GCs ---> 144518 ms: Mark-sweep 1317.0 (1404.4) -> 1317.0 (1404.4) MB, 1522.9 / 0.0 ms [allocation failure] [GC in old space requested]. 146029 ms: Mark-sweep 1317.0 (1404 ...

javascript for accessing JSON data both forwards and backwards

I am attempting to implement Next/Previous buttons for a json array, but I am encountering difficulties in making it work. Here is my latest attempt: <div id="text"></div> <button name="prev">go to previous div</button> <but ...

Node.js Implementation of HTML Content

Currently, I am attempting to iterate through a list of items and generate HTML code to be passed into the view file: const itemWrap = '<div id="items"></div>'; userDetails.notes.forEach(item => { const itemE ...

What is the best way to instruct jQuery to disregard an empty server response?

After examining this piece of code: $.ajax({ type: "POST", url: theRightUrl, data: whatToPost, logFunction: whatever, suppressSuccessLogging: !0, dataType: "html" }); I encountered an issue where Firefox displays a "no element ...

Tips on creating Twitter Bootstrap tooltips with multiple lines:

I have been using the function below to generate text for Bootstrap's tooltip plugin. Why is it that multiline tooltips only work with <br> and not \n? I would prefer to avoid having any HTML in my links' title attributes. Current Sol ...