Achieve a smooth scroll effect on a div's content when hovering over an image

I am facing a challenge with a div element that has limited height and width, but its content exceeds the size of the div. To address this, I have placed two images at the top and bottom of the div. My intention is to allow the content to scroll up and down based on the image I hover over. While I have managed to achieve the hovering effect, it seems uncontrolled as the scrolling continues till the end even if I move my mouse away from the image. I would like the scrolling to stop immediately when I move my mouse away, and resume from where it left off when I hover over the image again. jsfiddle

HTML:

<div id="hover"> HOVER ME </div>

<div id="container">

The Mauritius Blue Pigeon is an extinct species of blue pigeon formerly endemic to the Mascarene island of Mauritius in the Indian Ocean east of Madagascar. It has two extinct relatives from the Mascarenes and three extant ones from other islands. ...

# CSS code removed for brevity

JAVASCRIPT:

$('#hover').hover(function(){
    $('#container').animate({ scrollTop: $('#container')[0].scrollHeight }, 1000);
});
$('#hover2').hover(function(){
    $('#container').animate({ scrollTop: 0 }, 1000);
});     

Answer №1

In order to create a smooth animation effect, it is best to animate in small increments.

var amount = '';

function scroll() {
  $('#container').animate({
    scrollTop: amount
  }, 100, 'linear', function() {
    if (amount != '') {
      scroll();
    }
  });
}
$('#hover').hover(function() {
  amount = '+=10';
  scroll();
}, function() {
  amount = '';
});
$('#hover2').hover(function() {
  amount = '-=10';
  scroll();
}, function() {
  amount = '';
});
#hover {
  width: 200px;
  height: 20px;
  border: 1px solid #cc0000;
}

#hover2 {
  width: 200px;
  height: 20px;
  border: 1px solid #cc0000;
}

#container {
  width: 500px;
  height: 300px;
  overflow-y: auto;
  border: 1px solid #000;
  overflow: hidden;
}

#container div {
  width: 100px;
  height: 100px;
  float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="hover"> HOVER ME </div>

<div id="container">

  <...Content about the Mauritius Blue Pigeon...>

</div>
<div id="hover2"> HOVER ME </div>

Check out the demo at http://jsfiddle.net/gaby/xmAvh/

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

Retrieve the value from an object using a dot notation in its title

Currently, I am experimenting with creating a Facebook chatbot. To communicate with Facebook, I need to verify that I own the URL provided to Facebook in the Webhooks panel. I have set up an express route that appears as follows: app.get('/api/verif ...

Experiencing challenges in integrating fundamental Javascript elements on a chat page

My chat page skeleton is in place, but I'm facing challenges in connecting all the pieces. My goal is to send messages to the server whenever a user clicks 'send', and to update the displayed messages every 3 seconds. Any insights, tips, or ...

Encountering issues when trying to show a JSON-formatted list on an HTML page, preventing it

I'm currently encountering an issue with displaying JSON formatted data in an HTML file. The code seems to work fine, but the list is not being displayed. Here's a snippet of my code... import { Component } from '@angular/core'; import ...

What could be the reason for the malfunction of this angular binding?

Looking at the HTML below: <input type="checkbox" name="person" [(ngModel)]="person.selected" /> This input is part of a ngFor loop. Testing has revealed that despite some values of selected being true and others false, all checkboxes appear check ...

Add more functionality to the server.js script

I have the server.js file, which serves as the entry point for my Node application and is responsible for invoking three different functions (these functions are only called once when the server is up, such as creating child processes, validation, etc), wh ...

What causes .obj files to not respond to directional light in three.js?

Can anyone explain why the .obj file is not displaying directional light similar to the box? Both have identical materials. You can find the code on GitHub: https://github.com/triple-verge/triple-verge-website/blob/master/src/js/modules/logo-3d.js#L52 H ...

Utilize the WebGLRenderTarget again

In my project, I am working with two scenes: the main scene which displays a textured plane, and a secondary scene that needs to be rendered to a texture. This texture will serve as a map for the main scene's plane. Although most THREE.WebGLRenderTar ...

A method of iteration that allows us to traverse through an object, regardless of whether it contains a single item or an array of items, is known as dynamic looping

I am dealing with a JSON output that can have different types of data: There may be an array of objects like this: var data = { "EARNINGS": [ { "PAYMENT": "1923.08", ...

Angular cookies may expire, but using the back button always revives them

Utilizing angular's cookie library, I have successfully set a cookie to store the id and passcode of a shopping cart on the backend. However, despite setting the expiration date to a past time in order to expire the cookie once the cart is purchased, ...

Navigation and Cart Menu Issue: Inability to Expand or Collapse

The website I am currently working on is . I'm facing an issue where the cart menu expands but cannot collapse and the navigation menu does not expand when I resize the window for mobile view. Everything works fine in the normal view of the window. Ho ...

While developing a scene switch program in JavaFX, I encountered an error when attempting to switch scenes. Interestingly, the error disappears when CSS is not used – the program works perfectly without

Here is My Main.java File import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.*; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { try { ...

How to toggle the visibility of specific div elements within a v-for loop depending on their content?

I am working on a scenario where I have a collection of objects displayed in a v-for loop. Each object has a specific key value pair, and I want the user to be able to toggle a button outside the loop to show or hide elements based on that key value. Initi ...

The functionality of the Bootstrap carousel appears to be malfunctioning

My attempt at creating a carousel has hit a snag - it seems to be stuck on the first image and the next and previous buttons aren't functioning. I've gone over the code multiple times, but I just can't pinpoint the issue. Any assistance woul ...

"Enhance Your Text Fields with Angular2 Text Masks for Added Text Formatting

Is there a way to combine text and numbers in my mask? This is what I am currently using: [/\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/] The above code only allows for numbers. How can I modify it to allow f ...

Issues with events not properly attaching to elements within Angular Directives

In my current Angular app project, I've encountered an issue with ng-click not binding to an element that is brought in via a Directive. The app itself is focused on goal planning and this particular section tackles the obstacles individuals face when ...

Loading data from the database to populate DropDownListFor

Having trouble populating two dropdown lists with values from the database. The stored procedure is returning correct results and everything seems fine up to the controller level. I'm stuck on the syntax for @Html.DropDownListFor. What I have doesn&a ...

Endless scrolling with redux and react

I'm facing an issue while trying to implement infinite scroll in a React-based application that utilizes Redux for state management. I am attempting to dispatch an action on page scroll but have been unsuccessful so far. Below is my code snippet: // ...

Vitest does not currently support the experimental syntax 'jsx' in use

Currently utilizing Vite with the vitest package Error: Details: ...

What is the correct way to specify a property for a Type within a function component? Issue TS2339 encountered

Looking at the code provided below, we can see an implementation in React that works well in JS/JSX but encounters errors when using TypeScript. import React, { useContext, useReducer, useEffect, } from 'react'; const AppContext = React.createC ...

Discovering the most efficient route among a collection of objects in JavaScript

Presented here is an array of objects: const data = [ { start: "X", end: "Y", distance: 5}, { start: "X", end: "Z", distance: 4}, { start: "Z", end: "Y", distance: 8}, { start: "Y", end: "V", distance: 9}, { start: "V", end: "Z", dista ...