Ways to conceal the picture

Check out the JSfiddle link here for the full code.

I'm having trouble with my slider as the last picture keeps collapsing underneath and is not hidden as it should be. I suspect this issue is causing the slider to malfunction.

HTML

<div class="gallery-wrap">
  <div class="gallery clearfix">


     <div class="gallery__item">
          <img src="http://gallery.photo.net/photo/8439353-md.jpg" class="gallery__img" alt="" />
     </div>
     <div class="gallery__item">
      <img src="http://gallery.photo.net/photo/8439353-md.jpg" class="gallery__img" alt="" />
     </div>

     <div class="gallery__item">
      <img src="http://gallery.photo.net/photo/8439353-md.jpg" class="gallery__img" alt="" />
     </div>

    <div class="gallery__item">
      <img src="http://gallery.photo.net/photo/8439353-md.jpg" class="gallery__img" alt="" />
    </div>

    <div class="gallery__item">
      <img src="http://gallery.photo.net/photo/8439353-md.jpg" class="gallery__img" alt="" />
    </div>

CSS

.gallery-wrap { 

    margin: 0 auto;
    overflow: hidden;
    width: 432px;
}
.gallery { 
    position: relative;
    left: 0; 
    top: 0; 
}
.gallery__item { 
    float: left; 
    list-style: none;
    margin-right: 20px;
}
.gallery__img { 
    display: block;
    border: 4px solid #40331b;
    height: 80px; 
    width: 80px; }

.gallery__controls { margin-top: 10px; }
.gallery__controls-prev { cursor: pointer; float: left; }
.gallery__controls-next { cursor: pointer; float: right; }

.clearfix:after{
    content: '.';
    clear: both;
    display:block;
    height: 0;
    visibility: hidden;
}

JavaScript

$(window).load(function(){
    $(".gallery__link").fancybox({
        'titleShow'     : false,
        'transitionIn'  : 'elastic',
        'transitionOut' : 'elastic'
    });
    var totalWidth = 0;

    $(".gallery__item").each(function(){
        totalWidth = totalWidth + $(this).outerWidth(true);
    });

    var maxScrollPosition = totalWidth - $(".gallery-wrap").outerWidth();

    function toGalleryItem($targetItem){
        if($targetItem.length){
            var newPosition = $targetItem.position().left;
            if(newPosition <= maxScrollPosition){


                $targetItem.addClass("gallery__item--active");


                $targetItem.siblings().removeClass("gallery__item--active");
              $(".gallery").animate({
                    left : - newPosition
                });
            } else {

                $(".gallery").animate({
                    left : - maxScrollPosition
                });
            };
        };
    };

       $(".gallery").width(totalWidth);
    $(".gallery__item:first").addClass("gallery__item--active");


    $(".gallery__controls-prev").click(function(){
        var $targetItem = $(".gallery__item--active").prev();
        toGalleryItem($targetItem);
    });


    $(".gallery__controls-next").click(function(){
        var $targetItem = $(".gallery__item--active").next();
        toGalleryItem($targetItem);
    });
});

Answer №1

Some updates and improvements:

.gallery__item {
    display: inline-block;
    list-style: none;
    margin-right: 20px;
}

.gallery {
    position: relative;
    overflow: hidden;
    left: 0;
    top: 0;
    white-space: nowrap;
}

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

"Scotchy McScotchface's to-do list application powered

How is the index.html (frontend Angular) being triggered? The tutorial mentioned that by including one of the following routes in route.js, the frontend gets called app.get('*', function(req, res) { res.sendfile('./public/index.html&ap ...

Automatically save user input in form fields with the help of jQuery and AJAX technology

I'm working on a form that has various input fields, and I need the data entered by the user to be automatically stored in the database every minute. After the user submits the request, it will be processed in a struts file where database interactions ...

After making 5 Ajax get requests, there is no response being received

Currently, I'm facing an issue when trying to retrieve information from the MongoDB server to the frontend using an AJAX GET request. Everything works smoothly until I attempt to call the JavaScript function multiple times. Strangely, if I call the fu ...

Prevent leaving the body empty while populating a page with Ajax requests

I'm currently facing a dilemma at work. Our team is utilizing Bootstrap, jQuery, and a REST API to populate our web pages. Here's the sequence of events during page loading: The server receives a request A template is served which loads all ne ...

How can I convert JSON data from an array to an array of objects using JavaScript?

My goal is to load data into an ag-grid. I have obtained the json data in the following format: {"my_data":{"labels":[1,2,3,...], "idx":["idx1", "idx2", ...]}} In order to pass it to the grid, I need to transform it to this format: {"my_data":[{"labels" ...

How to direct all wildcard paths to a particular route in Next.js

I currently have a single landing page application built with nextJs. I am wondering if it is possible to redirect all paths to specific routes, similar to how we do it in react-router. How can I achieve the same functionality in nextJs? <BrowserRou ...

Is it acceptable to replicate another individual's WordPress theme and website design in order to create my own WordPress website that looks identical to theirs?

It may sound shady, but a friend of mine boasts about the security of his WordPress website, claiming it's impossible to copy someone else's layout or theme. However, my limited experience in web development tells me otherwise. I believe it is po ...

Inaccuracies arise when trying to interpret a JSON string as an object

I have tried various solutions found on stack overflow, but none of them seem to work for me. I am attempting to call an asmx web service using jQuery. Below is my code: <script type="text/javascript"> $(document).ready(function () { ...

Setting up Jplayer as an audio player: A step-by-step guide

I am looking to incorporate a Jplayer audio player into my project, but I am struggling to find any documentation or resources that provide instructions on what components to include and how to set it up. If anyone has experience with using the Jplayer au ...

Having trouble with your jQuery animation on a div?

Check out this jsFiddle example: http://jsfiddle.net/uM68j/ After clicking on one of the links in the demo, the bar is supposed to smoothly slide to the top. However, instead of animating, it immediately jumps to the top. How can I modify the code to ac ...

What is the best way to encode data for transmission from jQuery to a PHP file?

Attempting to send a form over to a PHP file through jQuery has been causing me trouble. The challenge lies in the fact that the content, which needs to be sent to the PHP file, contains slashes (/) due to BBcode being present within it. I've attempte ...

When refreshing, the useEffect async function will not execute

Upon page load, the getImages function is intended to run only once. After refreshing the page, both tempQuestionImages and questionImages are empty. However, everything works perfectly after a hot reload. I am utilizing nextJs along with Firebase Cloud ...

Using jQuery AJAX to send data containing symbols

When making an AJAX call, I am including multiple values in the data like this: var postData = "aid="+aid+"&lid="+lid+"&token="+token+"&count="+count+"&license="+license; postData = postData + "&category="+category+"&event_name="+e ...

Tips for centering a button within the body of a webpage

Despite my efforts, the alignment is not working as expected. The CSS I am using specifies that every <div> button inside the body should be centered both vertically and horizontally. However, it seems like something is preventing this from happeni ...

Setting dynamic routes in Next.js when the basePath is not the root can be done by carefully configuring the routing system

After following a tutorial on setting up my Next.js project to be located in a subfolder (/app), everything was running smoothly. However, I encountered issues when trying to use dynamic routes for certain pages. I attempted three different approaches: ...

Troubles with npm installation of jsdom on Ubuntu 14.04 OS

I have been struggling to install jsdom in my script, encountering an error that I can't find a solution for. Most people face issues with g++ not being installed, but I already have it installed. Here is the content of my package.json file: { "na ...

Utilizing Cell References in the Table Component of React Material UI

I'm exploring React and aiming to create an editable table that dynamically updates the $/Unit cell based on changes in the Price and Units cells. I'm having trouble figuring out how to access values from other cells. Can this be achieved using ...

The vertical baseline alignment is not functioning as expected

In my setup, I have a straightforward configuration: <button> Lorem </button> <input type="text" value="Ipsum"> both positioned side by side with the help of display: inline-block: button, input{ height: 34px; line-height: ...

CloudFront for Amazon - Limit MP3 playback to designated website

I've been trying to find a solution for allowing mp3 files in an Amazon S3 bucket paired with Cloudfront to be streamed directly on my site without revealing the source URL of the mp3s. I want to prevent others from sharing or leeching the link by vie ...

How can checkboxes be combined from two separate tables?

Within this interactive code example, you will find two tables containing checkboxes in each row. Upon clicking the To button, a dialog box will appear allowing you to select users or groups from a drop-down menu. The corresponding tables will then be dis ...