What is the best way to have the slide shift to the left solely after the initial click and prevent it from moving left on subsequent clicks?

Is there a way to prevent the slide area from scrolling left when the left button is clicked before it has been scrolled right first? Additionally, how can we lock the scroll after it reaches the last div?

var $slider = $("#recent-container")

$("#right-button").click(function() {
  $slider.css("left", "-=932px")
});

$("#left-button").click(function() {
  $slider.css("left", "+=932px")
});
/*----------Recent Projects----------*/

#recent-title {
  font-size: 30px;
  font-family: 'calibri light';
  border-bottom: #d8d8d8 solid 1px;
  margin: 0px;
  padding: 10px;
  box-shadow: 0 10px 10px -5px rgba(0, 0, 0, 0.3);
}

.thmbnl-name {
  font-size: 20px;
  font-family: 'calibri light';
  text-align: center;
}

#recent {
  text-align: center;
  border: #d8d8d8 solid 1px;
  overflow: hidden;
  position: relative;
  width: 950px;
  height: 330px;
  background-color: white;
  z-index: 1;
  margin-bottom: 25px;
}

#recent #recent-container {
  position: absolute;
  white-space: nowrap;
  left: 0;
  -webkit-transition: all 2s ease-in-out;
  transition: all 1s ease-in-out;
  z-index: -1;
  padding: 10px;
}

#recent #recent-container .thmbnl-recent {
  display: inline-block;
}

.thmbnl-recent {
  padding: 19.5px;
}

.thmbnl-recent:hover {
  background-color: #eaeaea;
  transition: background-color ease 1s;
}

#right-button {
  font-family: 'calibri';
  font-weight: bold;
  border-style: none;
  width: 60px;
  height: 60px;
  font-size: 35px;
  background-color: rgba(0, 0, 0, 0.6);
  position: absolute;
  right: 0px;
  top: 37.5%;
  cursor: pointer;
  color: #e5e5e5;
  border-radius: 100%;
  text-align: center;
  line-height: 55px;
}

#left-button {
  font-family: 'calibri';
  font-weight: bold;
  border-style: none;
  width: 60px;
  height: 60px;
  font-size: 35px;
  background-color: rgba(0, 0, 0, 0.6);
  position: absolute;
  left: 0px;
  top: 37.5%;
  cursor: pointer;
  color: #e5e5e5;
  border-radius: 100%;
  text-align: center;
  line-height: 55px;
}
<div id="recent">
  <h2 id="recent-title">Recent Projects</h2>
  <div id="recent-container">
    <div class="thmbnl-recent">
      <img src="http://placehold.it/190x190">
      <h1 class="thmbnl-name">Sample</h1>
    </div>
    <div class="thmbnl-recent">
      <img src="http://placehold.it/190x190">
      <h1 class="thmbnl-name">Sample</h1>
    </div>
    <div class="thmbnl-recent">
      <img src="http://placehold.it/190x190">
      <h1 class="thmbnl-name">Sample</h1>
    </div>
    <div class="thmbnl-recent">
      <img src="http://placehold.it/190x190">
      <h1 class="thmbnl-name">Sample</h1>
    </div>
    <div class="thmbnl-recent">
      <img src="http://placehold.it/190x190">
      <h1 class="thmbnl-name">Sample</h1>
    </div>
    <div class="thmbnl-recent">
      <img src="http://placehold.it/190x190">
      <h1 class="thmbnl-name">Sample</h1>
    </div>
    <div class="thmbnl-recent">
      <img src="http://placehold.it/190x190">
      <h1 class="thmbnl-name">Sample</h1>
    </div>
    <div class="thmbnl-recent">
      <img src="http://placehold.it/190x190">
      <h1 class="thmbnl-name">Sample</h1>
    </div>

  </div>

  <p id="right-button">></p>
  <p id="left-button">
    <</p>

</div>

Answer №1

To update the button functions so they only modify the CSS when recent-container is not positioned at the end, consider this example for the right button:

  var $slider = $("#recent-container");
  $("#right-button").click(function() {
    if ($slider.position().left + $slider.width() > 0) $slider.css("left", "-=932px");
  });

  $("#left-button").click(function() {
    if ($slider.position().left < 0) $slider.css("left", "+=932px");
  });

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

Utilizing Jquery to retrieve an array as the return value from a $.post request

After going through numerous discussions on this topic, I'm still struggling to make it work. However, I've made significant progress. I have a scenario where I send data from jQuery to my database. The database returns a record consisting of two ...

Creating a custom color palette with Material UI using ThemeProvider

Attempting to set a custom color for a Button using createMuiTheme and ThemeProvider has been successful with the primary and secondary palettes. However, when trying to utilize an alternate color such as "info", it does not seem to work as expected: http ...

Issue with event not functioning properly on a block inserted through an ajax call

Utilizing Thymeleaf and JavaScript together has been a game-changer for me. <form class="row" id="testamentExecutor"> <div id="executorsSection" class="form-row"> <div th:insert="fragm ...

The impressive wipe effect in velocity.js using css and jquery

I am looking to achieve a slide effect similar to Powerpoint's wipe using css, jquery, or velocity.js. Here is a video showing the desired effect: https://www.youtube.com/watch?v=VSBB0wfccws I attempted to use css3 masking (svg/gradient) but was uns ...

Tips on resizing images in CSS with accompanying text to prevent overlap

In my school project, I am using cloud9. When utilizing the live preview in a small window pane, my images appear correctly sized. However, upon maximizing my screen browser, the images stack on top of each other and their dimensions increase. My ideal l ...

Persistent column menu in ag-grid

Is there a way to include a menu for each row within a sticky column in Ag-grid? I couldn't find any information about this feature in the official documentation, so I'm unsure if it's even possible. I've attempted several methods, but ...

Initial variables for jQuery widget encryption setup

I have been searching for a viable solution to this dilemma for quite some time now, but I have yet to find any satisfactory answers. My goal is to develop a jQuery widget that customers can integrate into their websites. However, I need this widget to be ...

Choose all div elements except for those contained within a certain div

Having trouble with an IE7 z-index fixer that's affecting specific elements. I'm attempting to select all divs, excluding those nested within a particular div. Below is the jQuery code I'm using, but it's not functioning correctly: & ...

What is the best way to alternate between displaying HTML content with v-html and plain text in Vue.js?

I need a way to switch between v-html and plain text in Vue.js v2. Here's what I have so far: HTML <div id="app"> <h2 v-html="html ? text : undefined">{{html ? '' : text}}</h2> <button @click=&qu ...

Repetitive calling of a Node.js function

Currently, I am developing using the nodejs express framework. On my webpage, there are two buttons: 1) Submit, which triggers the following function: router.get('/record_enrich_quick/:quick', function(req, res) { console.trace(); var j ...

What is the recommended method for incorporating a frontend npm package that includes both CSS and JavaScript?

Recently, I've started using NPM and Gulp in my workflow. Up until this point, I've only dealt with NPM packages that consisted of either only JavaScript or CSS/SASS. Typically, I would either copy the package to a vendor directory to work with i ...

Pass intricate JavaScript object to ASP.Net MVC function

It appears that many people have shared helpful answers on a common topic, but I am still facing difficulties in making my attempt work. The issue is similar to the one discussed here, however, I am only trying to send a single complex object instead of a ...

Unlinked Checkbox in Angular Bootstrap Modal Controller

I am currently utilizing the modal feature from Bootstrap 3's Angular fork, and I am facing an issue with using a checkbox within the modal and retrieving its value in my main controller. The checkbox value is properly bound in the view but not in th ...

Navigate to a fresh webpage and find the scrollbar sitting at the bottom

When launching a new website using my forum system, I want the scrollbar to automatically be at the bottom so users don't have to scroll down. I'm not experienced in JavaScript and need help creating the code for this feature. ...

Switch the CSS class with an HTML tag

Can someone help me transform this code snippet with Python 3 and Beautiful Soup? <span class="ld-nowrap"> 20th century’s </span> I need to convert it to: <em> 20th century’s </em> Any suggestions on how to achieve t ...

Ways in which elements can be toggled through jquery or javascript?

There are various words listed below: word1 word2 word3 ... Every word in the list is linked to 1 to 3 examples. When a user clicks on a word, certain actions should take place. I want the examples to display when the associated word (e.g., word1) is c ...

Rather than overwriting it, append a new value to the localstorage

I am trying to insert the value of newUsername into the localStorage setUsernamesArray. However, my current code overwrites the existing value instead of adding to it. Code $('#signUpButton').click(function() { var newUsername = $('#userna ...

Enhanced Fancybox Version 2 adjusts iframe width to fit content

I have been attempting to adjust the width of the iframe in fancybox v2 to fit my content properly. However, every time I try, the content appears too large within the iframe and requires horizontal scrolling to view it all. My goal is to see the entire wi ...

Tips for fixing the issue of "The use of getPreventDefault() is outdated. Please use defaultPrevented instead."

When attempting to fetch data for a specific user from an SQL Server database using JSON data, I encountered an error message in the console: "Use of getPreventDefault() is deprecated. Use defaultPrevented instead." Additionally, the values are not bei ...