Slideshow development with code

This is the code I currently have:

HTML:

<head>
    <title>JQuery demo</title>
    <script type="text/javascript" src="scripts/jQuery.js"></script>
    <script type="text/javascript" src="scripts/slider.js"></script>
    <link rel="Stylesheet" type="text/css" href="styles/style.css"/>
</head>
<body>
    <div id="slider">
        <img class="image" src="images/image1.jpg" alt="image"/>
        <div class="title">Title1</div>
        <div class="bulletContainer">
            <div class="bullet active"></div>
            <div class="bullet"></div>
            <div class="bullet"></div>
            <div class="bullet" style="clear: right;"></div>
        </div>
        <div class="caption">Subtitle1</div>
    </div>
</body>

CSS:

body { 
    font-family:Tahoma;
    background-color: gray; 
}
#slider {
    width: 733px;
    height: 395px;
    margin: 50px auto 0 auto;
    background-image: url("../images/billboards-bg.png");
    background-repeat: no-repeat;
}
#slider > .image {
    margin: 10px 6px 2px 6px;
}
#slider > .title {
    float:left;
    color: #5D5B5B;
    margin-left: 7px;
    font-size: large;
    font-weight: bold;
}  
#slider > .bulletContainer {
    float:right;
    width: 100px;
    height: 20px;
    margin-right: 7px;
}
#slider > .caption {
    clear:both;
    margin-left: 7px;
    padding-top: 2px;
}
#slider > .bulletContainer > .bullet {
    width:20px;
    height: 20px;
    background-image:;
    margin: 2px;
    float:left;
    background-image: url("../images/billboard-pager.png");
    background-position:  0 0;
} 
#slider > .bulletContainer > .bullet:hover {
    cursor: pointer;
    background-position:  0 -20px;
}
#slider > .bulletContainer > .bullet.active {
    cursor: pointer;
    background-position:  0 -40px;
}

Lastly, the main issue I'm facing is:

$(document).ready(function() {
    $(".bullet").click(function() {
        $(".bullet.active").attr("class", "bullet");
        $(this).attr("class", "bullet active");
        setContent($(".bullet").index(this));
    });
    var images = ["images/image1.jpg", 
                  "images/image2.jpg", 
                  "images/image3.jpg",
                  "images/image4.jpg"];
    var titles = ["Title1", "Title2", "Title3", "Title4"];
    var subtitles = ["Subtitle1", "Subtitle2", "Subtitle3", "Subtitle4"]

    function setContent(index) {
        $(".image").fadeOut("fast", function() {
            $(this).attr("src", images[index]);
            $(".image").fadeIn("fast");
            $(".title").text(titles[index]);
            $(".caption").text(subtitles[index]);
        });
    }
});

I am looking for a way to update the titles and captions when changing the image, but I am unsure how to achieve this.

Answer №1

Check it out here: http://jsfiddle.net/ubfLe/

Modify the function setContent:

function setContent(index)
{
    $(".image").fadeOut("fast", function()        
    {
        $(this).attr("src", images[index]);
        $(".title").html(titles[index]);
        $(".caption").html(subtitles[index]);
        $(".image").fadeIn("fast");
    });
}

Answer №2

To enhance the transition effect, consider adjusting the fadein and fadeout speed to milliseconds instead of using predefined terms like fast or slow.

For example, you could implement the following code snippet...

$(".image").fadeOut(1000, function()        
    {
        $(this).attr("src", images[index]);
        $(".image").fadeIn(1000);
    });

Furthermore, you can create a script that dynamically updates the title and subtitle based on the selected index.

$(".bullet").click(function(){
    setTimeout(function(){
        $(".title").html(titles[$.inArray($(".image").attr("src"), images)]);
    }, 1000);
});

This suggestion is purely conceptual. I haven't had the opportunity to test it due to time constraints, but I can refine it later if needed. Feel free to reach out if you find this information useful.

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

Height transition animation in Angular 2 - maintaining state consistency

I am currently working with an animation code that is linked to my component. animations:[ trigger('slide', [ state('show', style({ height: '*' })), state('hide ...

The functionality of my PHP code for email transmission is currently malfunctioning

I'm having trouble sending mail from my HTML form using PHP. I've uploaded the necessary files to my hosting server but the mail is still not being sent. Here is my HTML code: <!-- Form --> <form action="http://example.com/sendEmail.php ...

Rotating out of the element with the transform rotate effect

I have rotated the text by 270 degrees and now I am facing the challenge of aligning it within another div. Here is the original image before I rotated the text: Below is the code I used for the layout: #infoside { background-color: #00ff00; flo ...

Issues arise with the Node EJS module due to the malfunction of the include

Struggling with incorporating HTML snippets into my index.html, even after reviewing the EJS documentation. Directory Structure: project -public --assets ---css ---images ---js --Index ---index.html + index.css and index.js --someOtherPageFolder -views - ...

Verify if an item is currently within the user's view

I'm encountering a new issue with a menu I'm working on. My goal is to hide the menu when the user clicks outside of it. The challenge lies in the fact that the menu is always visible but translated to the right of the element, making it not dire ...

Learn the process of saving a file in localStorage and subsequently transmitting it to the server within an MVC framework

How can we effectively store a file in localStorage, retrieve it later, and send it to the server for saving? <input id="File1" type="file" onchange="saveLocal();" /> <button type="button" onclick="saveServer()">Sync</button> <script& ...

Is it possible to customize the Angular Kendo Grid to be non-scrollable and automatically adjust its height to fit the row contents?

I'm trying to create a Kendo Grid in Angular that is Non-Scrollable and adjusts its height to fit the row contents. However, my current implementation is not working as expected, as it still gives me a fixed height. <kendo-grid [data]="propertyVie ...

Creating diverse content for various tabs using JavaScript

I have developed a code that uses a for loop to generate tabs based on user input. var tabs = ""; var y = 1; for (var x = 0; x < tabNum; x++) { tabs += "<li class = 'tabbers'>" + "<a href='#tab'>Tab</a>" + "& ...

Clicking on the background does not alter the onclick function

Can anyone help me troubleshoot my code? My function load() isn't changing the background of .firstDiv for some reason. I've checked multiple times but I can't seem to spot any errors. function load() { document.getElementsBy ...

Is there a way to retrieve a CSS file using AJAX from a separate origin?

Whenever I am trying to load a CSS file via AJAX from my dreamhost account, I encounter the error message that says No 'Access-Control-Allow-Origin' header is present on the requested resource. After searching for solutions online, I stumbled upo ...

Determine if the input text includes a URL when a key is pressed using jQuery

I am currently working on a feature to detect URLs when users input content manually, paste a link, or a combination of both. My goal is to retrieve the contents of the URL, similar to how Facebook handles it, when the first detection occurs. Here is the ...

Is it possible to utilize retina images with CSS3's :before selector?

Can I adjust the size of an image inserted using :before? For example, if I want to use a high resolution image in the content below - how can I specify the dimensions? .buy_tickets_btn:before{ content: url(../images/buttons/pink_ticket_btn_bg.pn ...

Implement two different styles within an element's style properties

Welcome everyone, I have a question regarding adding marquee effects to a table row along with highlighting it. Is it possible to include moving text from left to right in the same active row? For example, let's say that the current time is 12:45 AM. ...

html table displaying incorrect data while using dynamic data in vue 3

example status 1 Hello there! I'm trying to create a table (check out example status 1 for guidance). Here's the code snippet I am using: <table> <tr> <th>Product</th> <th>Price</th> <th>Av ...

Internet Explorer 9 does not display content until the ajax/json function has finished executing

I have encountered an issue with my code regarding updating a div element. The first innerHTML call seems to be ineffective and does not render in the browser. However, the second innerHTML call works as expected by appending "Complete" once the ajax call ...

Using Node.JS, Sequelize, and Moment.JS to format dates

Seeking help on formatting a date loaded from Sequelize in my database. I'm working on a blog and need to display the creation date of an article. Here's my route: app.get("/", (req,res) =>{ Article.findAll({ order:[ [ ...

Arranging SVG icons in a row within the navigation bar

Having trouble aligning the icons side by side? Even with margin and padding adjustments, I couldn't get it right. I've scoured multiple sites for a solution but couldn't find one. As an attempt to fix this, I created a bar with aligned butt ...

Rotate the horizontal scroll of an inner div within an outer div using CSS

I am looking to create an outer div with horizontal scroll and inner div with vertical scroll where the inner div moves along with the outer div. <body> <div id="wrapper"> <div id="outer">content outer <div id="inner"&g ...

What is preventing me from closing modals when utilizing multiple ones at a time?

I'm currently working on setting up a Bootstrap 4 gallery. I want the images to be clickable and open in a larger version. Everything is working fine, but when I introduce multiple scripts, I can no longer close the modals. Here's the code I hav ...

Ways to update div content following a successful AJAX call

I have a form that utilizes AJAX requests, and when the input fields are not filled correctly and the submit button is clicked, error messages are displayed without refreshing the page. While this functionality works, a problem arises when the form is subm ...