"Showing Images in a Slider: A Step-by-Step Guide

I'm facing an issue with displaying images in a slider. The problem is that the images are stacking vertically instead of appearing side by side in a slideshow. Below is how I have structured my code:


@if (Model.Photos.Count > 0)
{

    <div style="padding:10px">
        <div class="slide-content" style="max-width:800px">
            @foreach (var photos in Model.Photos)
            {
                <img class="mySlides" src="@Url.Content(@photos.photo_url)" style="width:100%">

            }
            <div class="w3-center">
                <div class="w3-section">
                    <button class="w3-button w3-light-grey" onclick="plusDivs(-1)">❮ </button>
                    <button class="w3-button w3-light-grey" onclick="plusDivs(1)"> ❯</button>
                </div>
            </div>
        </div>
     </div>
}

CSS :

https://i.sstatic.net/qk6wZ.png

javascript:

https://i.sstatic.net/msXmq.png

Answer №1

After implementing the carousel using your provided code, I noticed that the only issue was with the absence of dots in the HTML structure. Once I eliminated all dot references from the JavaScript file, everything functioned as intended.

If you are still facing any problems, I suggest sharing the rendered HTML to further investigate. My testing did not reveal any image positioning issues.

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  //var dots = document.getElementsByClassName("Slides");
  if (n > x.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = x.length
  }
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  /*
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" w3-red", "");
  }
  */
  x[slideIndex - 1].style.display = "block";
  //dots[slideIndex - 1].className += " w3-red";
}
.slide-content {
  max-width: 300px;
  margin: auto;
  position: relative;
}

.mySlides {
  display: block;
}

.slide-content {
  margin: auto;
}

.w3-center {
  text-align: center !important;
}

.w3-section,
.w3-code {
  margin-top: 16px !important;
  margin-bottom: 16px !important;
}

.w3-light-grey,
.w3-hover-light-grey:hover,
.w3-light-gray,
.w3-hover-light-gray:hover {
  /* color: #000 !important; */
  background-color: #f1f1f1 !important;
}

.w3-btn,
.w3-button {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}

.w3-btn,
.w3-button {
  border: none;
  display: inline-block;
  padding: 8px 16px;
  vertical-align: middle;
  overflow: hidden;
  text-decoration: none;
  color: inherit;
  background-color: inherit;
  text-align: center;
  cursor: pointer;
  white-space: nowrap;
}

button,
html [type=button],
[type=reset],
[type=submit] {
  -webkit-appearence: button;
}


/* Custom styles for testing */

img {
  max-height: 300px;
  margin-left: auto;
  margin-right: auto;
}
<div style="padding:10px">
  <div class="slide-content" style="max-width:800px">
    <img class="mySlides" src="https://i.ytimg.com/vi/5Nj2BngIko0/maxresdefault.jpg">
    <img class="mySlides" src="https://images-na.ssl-images-amazon.com/images/I/51IwmuOPQyL._SL1052_.jpg">
    <img class="mySlides" src="http://i0.kym-cdn.com/entries/icons/original/000/016/546/hidethepainharold.jpg">
    <div class="w3-center">
      <div class="w3-section">
        <button class="w3-button w3-light-grey" onclick="plusDivs(-1)">❮ </button>
        <button class="w3-button w3-light-grey" onclick="plusDivs(1)"> ❯</button>
      </div>
    </div>
  </div>
</div>

Answer №2

You should consider incorporating the "Owl Carousel 2" plugin into your project; it is highly effective and user-friendly.

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

Fetching JSON data using Javascript/JQuery

My goal is to access JSON data from an online web service that queries a MySQL database for a specific string and use Javascript to present it on an HTML page. My challenge lies in effectively displaying the retrieved information. The relevant part of my ...

Is the :root selector truly devoid of value?

While I understand that :root can be used to select the html tag in an HTML file and the svg tag in an SVG file, I personally find it strange and not practical to apply the same styles to both HTML and SVG using :root. It seems like an unrealistic scenario ...

Issue with disabled dates not refreshing in the view after adding them via button click

I have been utilizing the vue-hotel-date-picker plugin and it's been functioning well. The problem arises when I use the disabled dates props; it disables the dates initially when initializing and updating on the calendar. However, when I update the d ...

After the rendering process, the React Component member goes back to a state of

One issue I encountered is related to a component that utilizes a separate client for making HTTP requests. Specifically, when trying to use the client within a click event handler, the call to this.client.getChannel() fails due to this.client being undefi ...

Exploring Vue and Webpack's tree shaking functionality, side effects management, and its impact on CSS: Loading unused component CSS

We are currently grappling with the challenge of effectively implementing tree shaking for CSS within Vue Single File Components using Webpack. Within our package.json file, we have specified: "sideEffects": ["*.css", "*.less","*.vue"], which appears to b ...

Retrieve the ID value of the paragraph that was clicked on using the right-click function

If I have text with this specific html markup: <p id="myId">Some text <span>some other text</span></p> Is there a way to retrieve the value of the id upon right-clicking on any part of the paragraph? Update: I'm simply pass ...

utilizing the dropdown label for validating the value of another dropdown

I'm facing a challenge with two dropdowns: <select name="first_value" id="first_value"> <option value="<?php print"$sum_jan" ?>">January</option> <option value="<?php print"$sum_feb" ?>">February</option ...

Encountering an issue where rendering a component named Exercise within the ExerciseList component is not allowed

The ExerciseList component is designed to display all the exercises that can be edited or deleted from the list. It returns the Exercise Component or function for this purpose. If anyone could take a look and help identify any mistakes in my code, it would ...

Offspring of div should have equal height without the top and bottom padding

Looking to ensure that the height of child divs matches the height of the parent divs. Currently, the divs expand as the page is resized. This is for search results, so there are multiple identical code blocks. function matchHeight() { $result = $(&a ...

Replacing URLs in Typescript using Ionic 3 and Angular

Currently facing some difficulties getting this simple task to work... Here is the URL format I am dealing with: https://website.com/image{width}x{height}.jpg My objective is to replace the {width} and {height} placeholders. I attempted using this func ...

Is there a specific method that can be implemented in Node.js to compare two strings and identify the common words or letters?

Looking for a solution: var str1="CodingIsFun"; var str2="ProgrammingRocks"; Is there any function that can provide a true value or flag in this case? ...

The buttons in the Bootstrap modal are unresponsive

I'm attempting to create a modal navigation bar for mobile view but encountering an issue. When I click on the icon bar or menu bar in mobile view, none of the buttons inside the modal are responding. I'm quite stuck on this and would appreciate ...

Tips on accessing a particular value from an HTML header

My current task involves using selenium to extract request headers from a web page. However, I am facing an issue where all request headers are being printed out, whereas I only need one specific value from them. Despite searching online, I have been unabl ...

Creating uniform-sized cards with CSS and Bootstrap

Is there a way to ensure consistent card size across all lines? I've noticed that the cards in the second line are taller. Can we base the height of all cards on this? https://i.sstatic.net/KgZwu.png Below is the HTML code: <div class="row"&g ...

Show Zeroes in Front of Input Numbers

I am working with two input fields that represent hours and minutes separately. <input type="number" min="0" max="24" step="1" value="00" class="hours"> <input type="number" min="0" max="0.60" step="0.01" value="00" class="minutes"> This se ...

Error: The document is unable to detect any input values

I've been struggling with this issue for quite some time now and I can't seem to figure it out, no matter how hard I try. It seems that my input field is not capturing the value entered by the user for some unknown reason. Let me share the code ...

Is there a way to attach event listeners to dynamically inserted elements within a Google Calendar interface?

This particular issue is closely related to a few other questions: Jquery adding event listeners to dynamically added elements Add Event Listener for dynamically created particular element using jquery If you want to see a working example, check out thi ...

Tips for coordinating web service requests in JavaScript

My control application utilizes asp.net webservices for functionality. A timer is set to perform an asynchronous webservice call every 5 seconds in order to update the screen. Additionally, there are user buttons that trigger parts of the screen to refres ...

Sending an array from one page to another with Vue

I'm facing the challenge of passing an array from one page to another. When accessing the next page on form submission using this.$router.push('path'), is there a way for me to also transfer the array so that it can be accessed on the new p ...

The asynchronous functionality for reading and writing files from 'fs' is not functioning properly

I've been working on a script that reads and writes files, and so far the functions are functioning properly. However, I am facing challenges in making the read/write functions asynchronous. The main issue arises when trying to write a file and then i ...