What is the best way to ensure that only a specific Div on the page is scrollable

My goal is to create a set of three div elements that can be scrolled by dragging the mouse. However, I am encountering an issue where the window is scrolling instead of the divs themselves. When I attempted to change the window.scrollTo function to $('.widgets).scrollTo, the debugger returned an error with little information provided. Ideally, I would like these grey div elements to be scrollable without displaying a scroll bar, allowing users to drag them vertically.

Here is the HTML code:

 <!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="index.js"></script>
<link href="style.css" rel="stylesheet">
</head>
<body class="overflowyhide">

<div id="header" class="overflowyhide">

</div>

<div id="body" class="overflowyhide">
    <div class="appcontainer">
        <div class="appcardser white">
            <p>App Card Ser</p>
            <div class="widgets" id="wone">WOne</div>
            <div class="widgets" id="wtwo">WTwo</div>
            <div class="widgets" id="wthree">WThree</div>
        </div>
        <div class="caui white">
        Caui
            <div id="widgetOne">
                WidgetOne
                <div class="closeCAUI">Close</div>
            </div>



        </div>
    </div>
</div>

</body>

</html>

CSS:

#header{
    outline: 1px solid red;
    background: black;
}

#body{
    outline: 1px solid blue;
    background: black;
}

body{

}

.white{
    color: white;
}

.overflowyhide{
    overflow-y: hidden;
}

#widgetOne{
    background: red;
    height: 50px;
}

.widgets{
    height: 400px;
    color: black;
    -webkit-user-select: none;
    -moz-user-select: moz-none;
    -ms-user-select: none;
    user-select: none;
}

#wone{
    background: #dedede;

}

#wtwo{
    background: #9E9E9E;
}

#wthree{
    background: #828282;
}

.appcontainer{
     margin-right: 60px;
     margin-left: 60px;
     position: relative;
}

.appcardser{
    position: relative;
    display: inline-block;
    vertical-align: top;
    white-space: normal;
    word-wrap: break-word;
    overflow:hidden; 
    transition:all 0.4s ease-in;
    z-index: 10;
}

.appcardser:hover{
    background: rgba(0,0,0,1);
    position: relative;
    display: inline-block;
    vertical-align: top;
    transition:all 0.4s ease-in;
    white-space: normal;
    word-wrap: break-word;
    overflow-y:scroll; 
    overflow-x:hidden;
    z-index: 10; 
}


.caui{
  position: absolute;
  height:350px;
  top:90%;
width: 100%;
left: 0%;
z-index: 100;
background-color: rgba(8,8,8,0.95);
transition:all 0.2s ease-in-out;
}

.cauitrans{
  position: absolute;
  height:350px;
  top:0%;
width: 100%;
left: 0%;
z-index: 100;
-webkit-filter: blur(20px);
    -moz-filter: blur(20px);
    -o-filter: blur(20px);
    -ms-filter: blur(20px);
    filter: blur(20px);
transition:all 0.2s ease-in-out;
}

.cauireal{
  position: absolute;
/*  height:350px;
*/  top:0%;
width: 100%;
left: 0%;
z-index: 100;
transition:all 0.2s ease-in-out;
}

.cauismall{
  position: absolute;
  height:15px;
  width: 15px;
  top:92%;
  left:2%; 
  z-index: 100;
   border-radius: 50%;
 border:1px solid rgba(255,0,0,1);
  background-color: rgba(255,0,0,1);
transition:all 0.2s ease-in-out;
}

.cauibig{
    position: absolute;
    top:10%;
    width: 100%;
    left: 0%;
    z-index: 100;
    background-color: rgba(8,8,8,0.9);
    transition:all 0.2s ease-in-out;
}

.contdisapper{
  opacity: 1;
  visibility: visible;
  transition:all 0.2s ease-in-out;
}

.contdisapperani{
  opacity: 0;
  visibility: hidden;
  transition:all 0.2s ease-in-out;
}

JS:

$(document).ready(function(){


$('#header').height($(window).height()*0.1);
$('#body').height($(window).height()*0.9);

$('.appcontainer').width($(window).width()*0.55);
$('.appcardser').width($(window).width()*0.55);
$('.cauibig').height($(window).height()*0.8);
$('.caui').height($(window).height()*0.8);

$('.appcardser, .appcontainer').height($(window).height()*0.9);

// $('.clicktransform2').click(function() {
//        $('.caui').toggleClass('cauibig');
// });

$('.caui').click(function(){
    var heightOne = $('#widgetOne').height();
    var appContainerHeight = $('.appcontainer').height();

    if (heightOne >= appContainerHeight*0.1 && heightOne <= appContainerHeight*0.3){
        console.log("one");
        console.log(appContainerHeight*0.1);
        console.log(appContainerHeight*0.3);
        $('.caui').css({top: "80%"});
    } 
    else if (heightOne > appContainerHeight*0.3 && heightOne <= appContainerHeight*0.5){
        console.log("two");
        console.log(appContainerHeight*0.3);
        console.log(appContainerHeight*0.5);
        $('.caui').css({top: "60%"});
    }
    else if (heightOne > appContainerHeight*0.5 && heightOne <= appContainerHeight*0.7){
        console.log("three");
        console.log(appContainerHeight*0.5);
        console.log(appContainerHeight*0.7);
        $('.caui').css({top: "40%"});
    }
    else if (heightOne > appContainerHeight*0.7 && heightOne <= appContainerHeight*0.9){
        console.log("four");
        console.log(appContainerHeight*0.7);
        console.log(appContainerHeight*0.9);
        $('.caui').css({top: "20%"});
    }
    else {
        $('.caui').css({top: "10%"});
    }
});

$(document).on('click', '.closeCAUI', function(){
    $('.caui').css({top: "90%"});
});

// $('.closeCAUI').hover(function(){
//  $('.caui').css({top: "90%"});
// })

var xpos = 0;
var ypos = 0;
var drag = false;


window.addEventListener('mousemove', function(e){
    if (drag == true){
        //console.log("drag is true");
        newx = document.body.scrollLeft + (xpos - e.pageX);
        newy = document.body.scrollTop + (ypos - e.pageY);
        window.scrollTo(newx, newy);
        console.log(newx);
    }
});

window.addEventListener('mousedown', function(e){
    drag = true;
    xpos = e.pageX;
    ypos = e.pageY;
});

window.addEventListener('mouseup', function(e){
    drag = false;
});

});

Jsfiddle : http://jsfiddle.net/whw4h421/

Answer №2

Who needs JQuery or Javascript when you have CSS?

All you need to do is master the art of using CSS to achieve your desired effect.

Make sure to correctly apply the overflow and position properties to both parent and child div elements.

Check out this demo for a visual representation of the solution.

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

Instructions on creating the effect of rainwater cascading over text and spilling onto a webpage

Is it possible to make the rain flow over my text? Below is the code snippet from my JSFiddle: function strop(cleft, ctop, d) { var drop = document.createElement('div'); drop.className = 'punct'; drop.style.left = cleft + & ...

Content below divs that are fixed and absolutely positioned

I have a design layout similar to this (a small snippet) and I am facing an issue where the text within .content is not clickable. The positioning of #gradient is fixed due to the background image, and #empty has an absolute position to cover the entire bo ...

Tips for resizing a responsive website

Is it strange to consider scaling down a fully responsive website? Our client insists that every website, including his other ones, should have a max-width of 980px like his main site. So, if the screen size exceeds 980px, how can I scale down the website ...

What steps can be taken to resolve the problem of CSS sprite causing blurry images on mobile devices when using background-position?

In the world of CSS, working with regular images is usually straightforward - setting the width to 50px will display them perfectly on both desktop and mobile devices without any issues as long as the image quality is good. However, when it comes to using ...

Kendo Grid with locked height

Using a grid with some elements locked, we have established a CSS-defined minimum and maximum height for the grid. .k-grid-content { max-height: 400px; min-height: 0px; } An issue arises when setting the height of the locked grid. If the grid&a ...

Having trouble setting a value with sendKeys in Firefox using C# Selenium, and JavaExecutor isn't solving the issue

Hello everyone, I'm facing a bit of a challenge here that I've been struggling with for some time now. I'm trying to create an automation strategy for a Trading Platform (specifically Trading212) using C# and Selenium. Everything was going ...

Combining Content on a GitHub Page

Within the <head> section of my index.html, I have successfully utilized these links on localhost: <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"> <link href=&apos ...

How can I identify the main text of a specific <MenuItem/> component in ReactJS Material-UI?

I've been working with the Material-UI Dropdown Menu component and I'm trying to figure out how to console log the primaryText of the selected <MenuItem/>. Can anyone provide guidance on how to achieve this? ...

Javascript: A guide on passing an object through multiple nested functions

Hey fellow developers, I'm facing a challenge in my code and seeking advice from the experts out there. I'm attempting to retrieve JSON data from a specific URL, as shown below, and utilize it in a React component outside of the getWeather() fun ...

How to disable the onChange event in PrimeNG p-dropdown?

I'm currently utilizing PrimeNG's dropdown component. Each option in the list includes an icon that, when clicked, should trigger a specific method. Additionally, I need to execute another method when the onChange event of the dropdown is trigger ...

Render and download the file concurrently while displaying the view in Express

I'm looking to accomplish a task in Express where I can render a file and download it simultaneously. My current code looks like this: res.attachment('filename.csv'); res.render('pages/result', { data }); However, with this setu ...

Variables in the $scope object in AngularJS

Within my $scope in angularJS, I am working with two types of variables. 1). $scope.name and $scope.title are linked to input boxes in the UI html code. 2). On the other hand, $scope.sum and $scope.difference are internally used within my JS code. I need ...

Convert the value of the <textarea> element to HTML encoding

Is there a way to fetch the most recent updated value entered in a textarea and encode it in HTML format? I usually use this code snippet to retrieve the value: $('textarea').val(); // works consistently across browsers However, if the value c ...

Prompt triggered within AJAX request

I am facing an issue with my ajax call in which I am trying to invoke a php function containing an alert. However, the alert is not getting triggered and instead it returns me the JavaScript code (visible inside the console). How can I successfully use an ...

Implement a dialog on top of a current web page, achieve php-ajax query result, and enhance with

My website features 'dynamic' content, where 'static' nav-buttons replace specific div contents upon clicking. While I am able to retrieve my php/ajax results in a dialog box, I am struggling with placing this dialog above my current p ...

Retrieve selected value from Angular strap select dropdown

Angular strap is being used and the select element appears as shown below: <button type="button" class="btn btn-default" ng-model="selectedObject" data-html="1" bs-options="object.title for object in my_objects" bs-select> Action ...

Obtaining the calculated background style on Firefox

Back when my userscript was only functional on Chrome, I had a setup where I could copy the entire background (which could be anything from an image to a color) from one element to another. This is how it looked: $(target).css('background', $(so ...

Only keep the href and target attributes (if target is not empty) in all anchor tags while removing all other attributes

My task involves manipulating an HTML string by removing all attributes from anchor tags, except for href and target (if target has a valid value). $content = '<p style="abc" rel="blah blah"> Hello I am p </p> <a href="https://example. ...

Share the connection to the MongoDB database with the models.js file

Here is the content of the app.js file: var express = require('express'); var path = require('path'); var mongoose = require('mongoose'); var bodyparser = require('body-parser'); var conn = mongoose.createConnecti ...

Issue with Rickshaw graph not updating in real-time

Here is my code for generating a graph: var allVenues = []; var seriesData = []; var callGraph = function () { var where = jQuery('#hdnVal').val(); jQuery.ajax({ url: "PopulateTable", type: 'GET', ...