Retrieve the current value of an item by scrolling through it

I have a functioning sample of a scrollable with item numbers 1 - 24. I am trying to retrieve the value of the current item, but my attempts to use alert have failed. How can this be achieved? Here is my code:

UPDATE QUESTION: I managed to obtain the index of the scrollable value, but I am struggling to find a way to get the value of every index. Is there any method to retrieve the value of each index in the code below?

UPDATE:

<script>
$(function() {
    // initialize scrollable with mousewheel support
    $(".scrollable").scrollable({ vertical: true, mousewheel: true });   
    $('#scroll').bind('mousewheel', function(e){
        if(e.originalEvent.wheelDelta < 0) {
            //scroll down
            console.log('Down');
            alert('Down');
        } else {
            //scroll up
            console.log('Up');
            alert('Up');
        }

        //prevent page fom scrolling
        return false;
    });
}); 
</script>

I have added this to my JavaScript and it is working now, but it only outputs "UP" and "DOWN". I cannot figure out how to get the exact value of the div. Any suggestions?

<!DOCTYPE html>
<html>
  <title>scroll</title>

    <!-- include the Tools -->
  <script src="http://cdn.jquerytools.org/1.2.6/full/jquery.tools.min.js"></script>

<style type="text/css">
  /* root element for scrollable */
  .scrollable {

  /* required settings */
  position:relative;
  overflow:hidden;

  /*
  vertical scrollables have typically larger height than width but
  not now
  */
  height: 17px;
  width: 700px;
  }

  /* root element for scrollable items */
  .scrollable .items {
  position:absolute;

  /* this time we have very large space for the height */
  height:20em;
  }
</style>
</head>
<body>

<!-- root element for scrollable -->
<div id="scroll" class="scrollable vertical">

  <!-- root element for the items -->
    <div class="items" style="top: 0px;">

        <div>

            <div class="item">
                1

            </div>
        </div>
        <div>
            <div class="item">
                2

            </div>
        </div>
        <div>
            <div class="item">
                3

            </div>
        </div>
         <!-- Add more item values here -->
      
    </div>
  </div>


<!-- Additional script for handling scrollable content -->
<script>
$(function() {
    // Initialize scrollable with mousewheel support
    $(".scrollable").scrollable({ vertical: true, mousewheel: true });

});
</script>


</body></html>

Answer №1

Avoid using the mousewheel event and instead utilize the onSeek method of scrollable. Below is a functional example, along with the code provided: http://jsfiddle.net/bluegeek9bluegeek9/b5xn5/

$(document).ready(function() {

  $(".scrollable").scrollable({ 
    vertical   : true,
    mousewheel : true,
    onSeek     : function(){
       console.info("current position is: " + this.getIndex());
       console.info('current value is:', $('#scroll div.items > div:nth-child(' + (this.getIndex()+1) + ') > div.item').text());
    }
  });   

});

Reference

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

Guide to automatically triggering overscrolling

Currently working on a kiosk webpage designed for an iPad. Discovered an app that enables fullscreen mode, but requires overscrolling at the bottom. Is there a method to automatically trigger this on my webpage? Attempted to achieve this using autoscroll ...

What is the appropriate HTTP status code for "item not found" when using RESTful ajax?

Imagine having a single-page web application featuring a list of items. Users can click on an item to view more information about it. The details of each item are loaded asynchronously via an AJAX GET request to the URL http://www.example.com/item/[id], an ...

Ways to create auto-suggest recommendations that exceed the boundaries of the dialogue container

Is there a way to position autosuggest suggestions above the dialog instead of within it, in order to prevent scrolling of dialog content? Check out this sandbox example for reference: https://codesandbox.io/embed/adoring-bogdan-pkou8https://i.sstatic.net ...

How can I adjust the number of columns displayed per page on a Bootstrap Studio website?

Currently, I am utilizing Bootstrap studio to design a website featuring multiple cards on each page. Initially, I incorporated cards from the sb-admin-2 template and everything was proceeding smoothly. In my bootstrap studio interface, the three-column ca ...

What could be causing the tooltip to appear in the wrong position

It may seem like a trivial issue, but I am struggling to get my tooltips to display in the correct position. No matter what I try, they always appear off position. I have searched for solutions and attempted to rearrange the order of scripts, add new scri ...

Tips on adjusting text size based on the browser window dimensions

There seems to be an issue with the text when the window size is decreased. It overlaps with the image instead of resizing accordingly. I have tried using media queries, VW, and VH units to adjust the text size based on the window size but it's not wo ...

Creating a display of divs that flow from left to right in each row, and then stacking the rows from bottom to top using CSS and jQuery

My current dilemma involves a collection of divs that must be displayed in a specific order, but with a rather intricate layout. Essentially, this is the structure of my HTML: <div> <div class="box">1 (first in list)</div> <di ...

Insert a percentage sign into a right-aligned text box

I am trying to figure out how to permanently display a % symbol at the far right side of a textbox that shows a percentage. Can anyone help me with this? ...

Is Bootstrap 4 only being utilized in a fraction of the project?

We have made the decision to streamline our site by utilizing only GRID and NAVBAR on the vast majority of pages. This eliminates the need to load unnecessary jQuery and bootstrap.min.js on every page. The only JavaScript required is for the NAVBAR functi ...

Execution of Ajax call fails to occur synchronously

I have created a unique weather website that utilizes the flickr API as well as the yahoo API to gather weather data. However, I am facing an issue where the ajax call from the yahoo API does not retrieve the necessary data in time for the content of the p ...

404 error when accessing static content in Struts2

My Apache configuration includes the following settings: RewriteEngine On ProxyPass / <a href="http://demo1.example.com:8080/myApp" rel="nofollow">http://demo1.example.com:8080/myApp</a> ProxyPassReverse / ....(same url as above) Here is ...

Clearing hoverIntent using jQuery

In my scenario, I have three items identified as X, Y, and Z. The functionality of hovering is controlled by the hoverIntent. When I hover on item X, a tooltip is displayed using the following code: jQuery('.tooltiper').hoverIntent({ ove ...

Tips for creating a full-width fixed header

I've implemented a fixed header in a bootstrap container and I'm attempting to make the header fill 100% of the width without any margins or paddings. Here's a snippet of the HTML structure: <div class="container-fluid"> <div c ...

What is the best way to retrieve the value of a chosen option utilizing Javascript or Jquery?

I have been working on a custom select box and I am trying to retrieve the value of the selected label. When I click the "check" button, an alert pops up showing the code inside the div, which makes sense. However, I am wondering how I can extract the valu ...

Control the playback of multiple HTML5 videos with the ability to pause and resume, while also adding a

Trying to achieve the functionality of playing and pausing multiple videos. When one video is played, the others should automatically pause. The videos should appear blurred, except when actively playing. Does this make sense? http://jsfiddle.net/1frjov8e ...

When using CSS float:left and overflow:visible, the text may get cropped-off at

I'm currently experimenting with creating a color gradient in javascript using numerical values within some of the divs to indicate scale. However, I've run into an issue where as the values get larger, they are cut off due to the float:left prop ...

Issue with box-sizing border-box not applying to top and bottom borders

Currently, I am in the process of developing my own responsive website. However, I have encountered an issue with the box-sizing border-box on the top and bottom borders of one specific image. The layout consists of two columns with images of varying heigh ...

Accessing the next and previous elements using jQuery

Aug: <input type="text" value="100000" name="targetMonth_8" id="targetMonth_8" class="targetMonth" disabled> Sep: <input type="text" value="100000" name="targetMonth_9" id="targetMonth_9" class="targetMonth" disabled> Oct: <input type="text" ...

Can we separate city, state, and country data into individual input fields using Autocomplete and Geonames?

Currently, I have a single INPUT field set up to trigger the jQuery UI Autocomplete function, which pulls data from Geonames API. $( "#city_state_country" ).autocomplete({ source: function( request, response ) { $.ajax({ url: "http ...

Variety of formatting options for menu items when the menu is in its collapsed state

I am working with a Bootstrap nav-bar that collapses into a button by default. Within my nav-bar, I have opted for images instead of text links. However, when the nav-bar is collapsed, I would like these images to be smaller in size. Is there a way to cu ...