What is the best way to add an undetectable scrollable section to an HTML page?

I am looking to create an event that triggers whenever the user scrolls up or down within a hidden div called a 'scroller'. Here is the setup:

CSS

#scroller {
    position: absolute;
    top: 0px;
    left: 0px;
    height: 100%;
    width: 50px;
}
#scroller div {
    position: absolute;
    top: 0px;
    left: 0px;
    height: 50000px;
    width: 100%;
}
span {
    position: absolute;
    top: 20px;
    left: 100px;
}

HTML

<div id="scroller"><div></div></div>
<span></span>

Javascript

var timeout;
$("#scroller").scroll(function ()
{
    clearTimeout(timeout);
    $('span').text('scrolling');
    timeout = setTimeout(function ()
    {
       $('span').text('');
    }, 1000);
});

When the user scrolls inside the above div, the word "scrolling" should be displayed on the screen. You can test it out with this fiddle : http://jsfiddle.net/f1hxndt4/4/

There are two issues with the current setup :

  1. The scrolling within the 'scroller' needs to be continuous in both directions (up and down) - Currently restricted to 50000px scroll.
  2. The "scroller" must be invisible. Currently, the scrollbars are visible.

Any helpful suggestions would be highly appreciated, thank you!

Answer №1

For those who are curious, here is the solution: http://jsfiddle.net/f1hxndt4/14/

CSS

#scroller{
    position: absolute;
    top: 0px;
    left: 0px;
    height: 100%;
    width: 50px;
    overflow: hidden;
}
#scroller .parent{
    position: absolute;
    top: 0px;
    left: 0px;
    height: 100%;
    width: 100px;
    overflow-x:hidden;
}
#scroller .child {
    position: absolute;
    top: 0px;
    left: 0px;
    height: 50000px;
    width: 100%;
}

span {
    position: absolute;
    top: 20px;
    left: 100px;
}

HTML

<div id="scroller">
    <div class="parent">
        <div class="child"></div>
    </div>
</div>
<span></span>

Javascript

var timeout;
$("#scroller .parent").scroll(function ()
{
    clearTimeout(timeout);
    $('span').text('scrolling');
    timeout = setTimeout(function ()
    {
       $('span').text('');
    }, 1000);
});

Explanation :

To achieve this effect, create a scrollable <div> : $('#scroller .parent') and nest it inside a narrower <div> : $('#scroller'). Ensure that the overflow of the outer div is set to 'hidden'.

By doing so, the scrollbar on the right side of $('#scroller .parent') will be concealed.

Answer №2

If you attach an event listener to the 'scroll' event, you will have to ensure that the area is scrollable, which may not align with your intended outcome. Rather than this approach, consider listening for events that typically trigger scrolling actions, such as mousewheel or swipe gestures.

To calculate the distance scrolled, you can utilize the wheelData property of the event object to determine the scroll delta. (For Firefox and Opera browsers, you may need to use the detail property instead.)

var onMouseWheelEvent = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll"
                                                               : "mousewheel";
var timeout;
$("#scroller").on(onMouseWheelEvent, function (e)
{
    clearTimeout(timeout);
    $('span').text('scrolling');
    
    var scrollEvent = e.originalEvent;
    var delta = scrollEvent.detail? scrollEvent.detail*(-120) : scrollEvent.wheelDelta
    console.log(e.originalEvent.wheelDelta);

    timeout = setTimeout(function ()
    {
       $('span').text('');
    }, 1000);
});

Answer №3

Example: http://jsfiddle.net/techmind/x4k6s3j9/ Enhanced Version: http://jsfiddle.net/techmind/x4k6s3j9/1/

This solution is similar to the original link you shared but it doesn't depend on the scrolling amount. Instead, it generates its own data based on mousewheel input. I aimed to address your initial issue directly.

If you have any questions or need clarification, feel free to ask. (Note: This code snippet does not utilize jQuery as part of a challenge)

var a=0, topSpeed = 20, deg=0;

window.addEventListener('mousewheel', function(e){
    if (a<topSpeed) {
        a = a + ((e.wheelDelta/1000) * topSpeed);
    }

});

var img = document.getElementById('gear');

function animate() {
    a = +(a*.95).toFixed(2);
    if (Math.abs(a)<1) a=0;
    deg = (deg+a) % 360;
    img.style.transform = 'rotate('+deg+'deg)';
    requestAnimationFrame(animate);
}

animate();

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

A few of the input fields I filled out are not getting sent to the PHP script

Currently, I am working on a school project that involves creating a website similar to IMDB. My task is to include comments in our database, but I am facing an issue where two fields (fid and spoiler) are not getting posted to the PHP script handling the ...

The submit function for Ajax is not functioning properly

<script> var markerLatitude; var markerLongitude; function initializeMap() { var centerCoordinates = new google.maps.LatLng(51.8979988098144, -2.0838599205017); var mapOptions = { zo ...

How can I use `app.js` in Zendesk to connect to an external API using the complete URL

As someone new to developing Zendesk apps, I've been following the step-by-step guide available here. To Summarize I'm facing an issue with passing external API URLs to the AJAX call syntax within Zendesk's app.js file. You can find my sim ...

Error encountered in Three JS Drag Controls: Unable to assign value to property 'x' as it is undefined

I've been trying to drag the spheres around the scene using drag controls that should be activated when the "m" key is pressed. However, I keep running into an issue where the objects don't move and I receive an error message saying "Uncaught Typ ...

Steps to substituting characters within a date string

Create a function called normalize that changes '-' to '/' in a given date string. For example, normalize('20-05-2017') should output '20/05/2017'. This is my attempt: let d = new Date('27-11-2021'); fun ...

The content in tinymce cannot be edited or removed

Is there a method to prevent certain content within the tinyMCE Editor from being edited or removed? While I know that adding a class "mceNonEditable" can make a div non-editable, it can still be deleted. Is there a way to make it unremovable as well? ...

What is the reason for AngularJS's inclusion of a colon at the end of a data object in an $http post

While attempting to utilize angular js $http for sending a post request to elasticSearch, I encounter an "Unexpected token : " Error. Here is a snippet of my code: var request= $http({ method: "post", url: path, accept:"*/*", headers:{"Co ...

Update the ng-model using an Angular service

I am currently working on an Angular application that includes a textarea: <textarea id="log_text_area" readonly>{{logger}}</textarea> In addition, there is a Logger service designed to update this textarea. angular.module('app').f ...

What is the process for implementing JavaScript in Django?

I'm a newcomer to this and although I've tried searching on Google, I haven't found a solution. I'm facing an issue where I can include JavaScript within the HTML file, but it doesn't work when I try to separate it into its own fil ...

Tips for adding a placeholder in a login form on Drupal 7

Can anyone help me set a placeholder for a horizontal login form in Drupal 7? I want the placeholder text to disappear when clicked on, and I want it to display 'username' and 'password'. Here is the current code for the form. Thank you ...

How is it possible for the web browser to display fonts like Open Sans even if they are not installed on my device?

Explaining how a browser is able to correctly display text with a specific Font-Family, it can be described as follows: The font-family property allows designers to create a prioritized list of fonts for the browser to use when displaying content. If the ...

What is the best way to position a div vertically on the right side of another div?

I've been experimenting with different padding and margins to try and position the div below the dotted div. My goal is to have the div that's currently at the bottom appear vertically aligned to the right of the other div, similar to the layout ...

Using jQuery to retrieve information and calculate total sum

I am currently attempting to calculate the total sum of checked input prices. Here is the code I am using: function totalSum(e) { e.preventDefault(); var unit = $("input:checked").parent("dt").siblings("dd").find("span"); total = 0; $ ...

One handy feature in PHP or JavaScript allows users to delete items by simply

I'm encountering an issue with removing checked items from the data because my delete button is not included in the for loop that retrieves data from the database. This makes it hard to identify which data needs to be deleted based on its ID. While I ...

Master the art of handling JSON data within an Angular controller

I've been spending a day attempting to manipulate a JSON in order to display a table, but I can't seem to achieve the desired outcome. My goal is to showcase statistics in a table for each town. If a town has multiple lines, I want to display a ...

"Expand" Button following X buttons

Check out this JSFiddle for the code: $(document).ready(function(){ $( ".keywordsdiv" ).each(function(){ $(this).children(".keywords").eq(3).after('<a href="" id="playtrailershowmorebuttons">....Show More</a>');//add a uniq ...

I am experiencing issues with Jquery ajax JSON not functioning properly on my webserver when accessing my pages

When utilizing the Jquery Ajax function to parse the last.fm API, everything runs smoothly. Here is the Jquery function: $.ajax({ url: "http://ws.audioscrobbler.com/2.0/?method=album.search&album=believe&limit=2&api_key=b25b959554ed76058a ...

What is the most effective method for maintaining a stable page connection?

Currently, I am in the process of developing a website using PHP and JQuery. I am looking to implement an automatic page content update feature that triggers whenever new data is fetched from the database. So far, my attempts with JQuery have led me to a ...

Need to double-click to open my component

Hey there, I just started learning Angular and I'm using Leaflet for my application. I've been able to create some markers, but now I want to open a different component when I click on one of them. This is the code I'm currently using for ...

Ways to center text within a nested div in a parent div with table-cell styling

The issue I am facing involves a parent div with the CSS property display: table;. Nested inside is a child div with display: table-cell;. Despite applying text-align: center;, the text within the second div does not align to the center as expected. Here ...