I want to create a custom jQuery slider totally from scratch

Greetings everyone,

I have been tasked with creating a slider using only HTML / jQuery code.

Here is the template:

And here is the HTML code for the template above:

<div id="viewport-container">
    <section id="sliding-container">
        <article id="slide-0" class="slide"><span></span></article>
        <article id="slide-1" class="slide"><span></span></article>
        <article id="slide-2" class="slide"><span></span></article>
        <article id="slide-3" class="displayed-slide"><span></span></article>
    </section>
</div>
<nav id="slider-nav">
    <a href="#slide-0" class="active"></a>
    <a href="#slide-1"></a>
    <a href="#slide-2"></a>
    <a href="#slide-3"></a>
</nav>

The functionality should be such that when button {#slide-0} is clicked, it will display article {ID="slide-0"}; subsequently, selecting another button like {#slide-3} will cause the article with {ID="slide-0"} to fade out and the article with {ID="slide-3"} to fade in. This pattern continues as users switch between buttons, causing articles to fade in and out accordingly.

I have been struggling with this task for about a week now, so I would greatly appreciate any assistance you can offer.

Thank you all very much

Answer №1

Here's a guide to get you started. You will require: - http://jquery.com/ - JQuery - http://jqueryui.com/ -JQueryUI

Using these two tools will simplify your work process. Begin by integrating JQuery into your website and then add JQueryUI.

Next, implement something similar to the following in your own JQuery Code:

The HTML:

<div id="slider">
    <div id="firstSlide">
        <img class="active" src="pics/home/1.1.gif"/>
        <img src="pics/home/1.2.gif"/>
        <img src="pics/home/1.3.gif"/>
    </div>
</div>

The CSS:

    #slider
    {
        position: relative;
        height: 180px;
        border-bottom: 3px solid black;
        z-index: 1;
        box-shadow: 0px 0px 10px black;
    }

    #firstSlide
    {
        position: absolute;
        width: 198px;
        height: 100%;
        left: 0%;
        border: 1px solid black;
    }

    #firstSlide img
    {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0px;
        left: 0px;
        z-index: 1;
    }

    #firstSlide img.active
    {
        z-index: 3;
    }

The JQuery:

var howLongEffectLasts = 1000;
var timerInterval = 7000;
var slideDelay = 300;
var slideEffect = 'slide';
var slideDirection = 'up';

var timer = setInterval('DoIt1()', timerInterval);

function DoIt1()
{
    var $active = $('#firstSlide' + ' .' + 'active');
    var $next = ($active.next().length > 0) ? $active.next() : $('#firstSlide' + ' ' + 'img:first');
    $next.css('z-index',2);
    $active.toggle(slideEffect, { direction: slideDirection }, howLongEffectLasts, function() {
        $active.css('z-index',1).show().removeClass('active');
        $next.css('z-index',3).addClass('active');
    });
    setTimeout(function() { DoIt1(); }, slideDelay);
}

Modify the variables in the JQuery code according to your requirements. Also, customize the CSS to suit your needs. Pay attention to the Z-INDEX values as they are crucial for proper functioning of the provided CSS.

Answer №2

Have you considered using a javascript function within your href attribute to dynamically change the class of your article?

By altering the class of your article, you can control whether it is displayed or not (for example, with classes like slide or displayed-slide).

Is this the approach you are looking for?

Have you experimented with any solutions so far?

Answer №3

Finally accomplished it! (exhales)

Below is the CSS styling:

#viewport-container {
height: 250px;
width: 980px;
margin: 0 auto;
overflow: hidden;
}

#sliding-container {
width: 100%;
}

#slide-0 {
background-image: url(/Images/slide_home.jpg);
height: 250px;
width: 980px;
position: absolute;
}

#slide-1 {
background-image: url(/Images/slide_informatica.jpg);
height: 250px;
width: 980px;
position: absolute;
}

#slide-2 {
background-image: url(/Images/slide_musica.jpg);
height: 250px;
width: 980px;
position: absolute;
}

#slide-3 {
background-image: url(/Images/slide_recensioni.jpg);
height: 250px;
width: 980px;
position: absolute;
}

#slider-nav {
text-align: center;
margin: 10px 0 0 0;
}

#slider-nav a {
width: 10px;
height: 10px;
display: inline-block;
background: #ddd;
border-radius: 50%;
}

#slider-nav a.active {
background: #999;
}

This snippet represents the HTML structure:

<head>
<title></title>
<script src="Scripts/jquery-2.0.3.min.js"></script>
<link href="CSS/Slider.css" rel="stylesheet" />

</head>
<body>
<div id="viewport-container">
<section id="sliding-container">
<article id="slide-0" class="displayed" style="display:block;"><span></span></article>
<article id="slide-1" style="display:none"><span></span></article>
<article id="slide-2" style="display:none"><span></span></article>
<article id="slide-3" style="display:none"><span></span></article>
</section>
</div>
<nav id="slider-nav">
<a id="btn-0" href="#slide-0" class="active"></a>
<a id="btn-1" href="#slide-1"></a>
<a id="btn-2" href="#slide-2"></a>
<a id="btn-3" href="#slide-3"></a>
</nav>

The following script controls the functionality:

<script>
$(document).ready(function () {
var $navButtons = $("#slider-nav > a");

$navButtons.click(
function () {
var $selectedButton = $(this);
var rawIdSlideToFadeIn = $selectedButton.attr("href");

$navButtons.removeClass("active");
$selectedButton.addClass("active");

crossFading(rawIdSlideToFadeIn);
});

function crossFading(rawIdSlideToFadeIn) {
var $slideToFadeIn = $(rawIdSlideToFadeIn);                
var $slideToFadeOut = $("article.displayed");
var idSlideToFadeIn = $slideToFadeIn.attr("id");
var idSlideToFadeOut = $slideToFadeOut.attr("id");                

if(idSlideToFadeIn != idSlideToFadeOut)
{
$slideToFadeOut.removeClass("displayed").css("style", "none").fadeOut();
$slideToFadeIn.addClass("displayed").css("style", "block").fadeIn();
}
}
});
</script>

Although there are areas for improvement, the core foundation has been established.

Special gratitude to SushiBalboha for steering me in the right direction.

Thank you sincerely

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

Tips for ensuring that Breadcrumbs are displayed properly with the noWrap feature

I am currently working on making the MUI component Breadcrumbs responsive. The issue I am facing is that when the Breadcrumbs component fills up all available space, the items shrink and use ellipsis like a Typography component with the noWrap property se ...

Creating a notification feature for an HTML application

I am in the process of creating an HTML app through intel XDK. While I understand that using HTML to build apps is not as common, it is the language I am most familiar with, along with CSS. One feature I would like to include in my app is a weekly notific ...

How to mock nested functions within sinon

There are several questions similar to this one, but none of them quite match the scenario I'm dealing with. The situation involves a function that takes another function as a parameter: var myfunc = (func_outer) => { return func_outer().func ...

From AJAX response to React state attribute, store the JSON data

I have a component where I need to fetch freight values via ajax and store them in the state property of this class. import React, { Component } from 'react'; import Freight from './Freight'; import CreateFreightEntryModal from '. ...

Working with dynamic checkbox values in VueJS 2

In my project using VueJS 2 and Vuetify, I am creating a subscription form. The form is dynamic and fetches all the preferences related to a subscription from the server. In the example below, the preferences shown are specifically for a digital magazine s ...

How can you switch between CSS styles using JQuery?

Is there a way to change CSS properties every time I click using jQuery? I couldn't find any information on this specific topic. Can someone guide me on how to achieve this with jQuery? I want the background color to change each time it is clicked. W ...

How to make an HTTPS REST API request in Node.js with JSON body payload

Currently, I am attempting to make a secure HTTPS REST request in Node.js by utilizing the following code: var querystring = require('querystring'); var https = require('https'); var postData = { 'Value1' : 'abc1&ap ...

Changes to the model cannot be realized unless $scope.$apply is used

Are there alternative methods to achieve the desired model change without utilizing $scope injection in an Angular "controller as" approach within the given setup? The HTML: <div data-ng-controller="Buildings as vm"> <select data-ng-model="vm. ...

After clicking multiple times within the modal, the modal popup begins to shift towards the left side

I successfully implemented a modal popup in my project, but encountered an issue where the popup moves to the left side if clicked multiple times. Despite searching extensively online, I have not been able to find a solution to this problem. Below is the ...

How can I load data into Vuex store before the application starts?

Within the created hook of App.vue, I am initiating a dispatch call to my store. Subsequently, in a child component, I attempt to retrieve this data using a getter. However, an issue arises as the child component is loaded before the data is stored, causin ...

Ways to dynamically change the textbox value without having to refresh the page by utilizing AJAX in MVC

I am looking to update the value of a textbox from a database without refreshing the page. I have attempted to do so using AJAX, and while it updates the value without a page refresh, it is not correctly binding to the textbox. My Controller public A ...

Utilizing image backgrounds for hyperlinks in the Android browser

Encountering a minor issue with some <a> tags that have images as background, and the text inside the tags is indented using CSS. However, on the Android browser, part of the string used in HTML appears to cover the background image within the area o ...

Executing Jquery ajax calls in sequence to ensure response order

I am facing a challenge in my plugin where I need to make sequential ajax calls and handle the responses accordingly. The issue is that the responses do not match the order of the calls, and using async: false is not an option for me. var dynamicValues = ...

Creating a new version of an existing method found within a component in a Vue store.js file

As I navigate through the learning curve of vue.js, a seemingly simple question has arisen: how can I achieve the following task? Within one of my vue components, I am facing challenges with the implementation of the "loadSuggestedUsers" method. Here is t ...

Troubleshooting IE compatibility for $.trim() jQuery functionality

Having trouble with $.trim() not working in IE but works fine in Firefox. Any ideas why this might be happening? Thanks. $('#GuestEmailAddress').on('blur', function () { var $inputValue = $(this).val(); ...

Trouble with jquery/ajax form submission functionality

I followed a jQuery code for form submission that I found on various tutorial websites, but unfortunately, the ajax functionality doesn't seem to be working. When I try to submit the form, nothing happens at all. I've tried troubleshooting in eve ...

Troubleshooting: Unable to modify value with function in AngularJS

Why can't I change a value using a function in AngularJS? html: <div ng-controler='TestCtrl' id='TestCtrl'> <h1>Test: {{test.name}}</h1> <div ng-hide='showTest'> <div class=&a ...

What are the best strategies for handling complex task operations in Node.js Express.js?

How can I effectively manage lengthy task functions in Node.js Express.js to prevent timeout errors? Currently, my application includes a time-consuming function that does not require an immediate response but still needs to execute its tasks. How can I en ...

Tips for integrating semantic HTML with React components

As a newcomer to react, I am eager to establish a strong foundation before delving deep into the language and risking having to backtrack later on. I am curious about how to incorporate semantic HTML in react. Elements such as <header>, <nav>, ...

`Can a creation of this nature be accomplished?`

In my text input field, users can type messages to send to me. I'd like to add buttons on the page with symbols like "!", "XD", and "#". When someone clicks on a button, such as the "!" button, that corresponding symbol should be inserted into the tex ...