What steps can I take to modify the peeking effect from hover to scrolling?

I have successfully integrated a peeking effect on my website. Check it out here: http://jsfiddle.net/7yrWL/1/ Currently, the effect triggers when I hover over the image. What I want now is for the peeking effect to only activate when the user scrolls to that section. In other words, the container should peek out when scrolled to, not just clicked.

Any suggestions or ideas?

Thank you

<div class="main square">
    <div>
        <div class="content">
            <h1>Title</h1>
            <h2>Subtitle</h2>
            <div class="peek">
                <p>Test peek test peek<br/>Test peek</p>
                <h3>MORE TESTING</h3>
            </div>
        </div>
    </div>
</div>
<div class="main">
    <div>
        <div class="content">
            <h1>Title</h1>
            <h2>Subtitle</h2>
            <div class="peek">
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <h3>MORE<br/>Peek</h3>
            </div>
        </div>
    </div>
</div>
<div class="main large">
    <div>
        <div class="content">
            <h1>Title</h1>
            <h2>Subtitle</h2>
            <div class="peek">
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <p>Description<br/>with<br/>many<br/>lines.</p>
            </div>
        </div>
    </div>
</div>

.main { padding: 10px; overflow: hidden; background-color: orange; color: white; height: 300px; width: 300px; display: inline-block; }
.main > div { position: relative; background-color: red; height: 100%; }
.main .content { position: absolute; bottom: 0; padding: 10px; right: 0; left: 0; }
.main .peek { max-height: 0; -webkit-transition: max-height 1s; -moz-transition: max-height 1s; transition: max-height: 1s; background-color: green; overflow:hidden; }
.main:hover .peek { max-height: 300px; } /* any way to make this 100% so it can fit any size? */

.main.large { height: 600px; width: 600px; }

Answer №1

$(window).scroll(function() {
  scrollEffect();
});

function scrollEffect() {
  $('.main').each(function(i) {
    if ($(this).position().top <= $(window).scrollTop()) {
      $(this).addClass('effect');
    }
  });
}
.main { padding: 10px; overflow: hidden; background-color: orange; color: white; height: 300px; width: 300px; display: inline-block; }
.main > div { position: relative; background-color: red; height: 100%; }
.main .content { position: absolute; bottom: 0; padding: 10px; right: 0; left: 0; }
.main .peek { max-height: 0; -webkit-transition: max-height 1s; -moz-transition: max-height 1s; transition: max-height: 1s; background-color: green; overflow:hidden; }
.main.effect .peek, .main:hover .peek { max-height: 300px; } /* any way to make this 100% so it can fit any size? */

.main.large { height: 600px; width: 600px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="main square">
    <div>
        <div class="content">
            <h1>Title</h1>
            <h2>Subtitle</h2>
            <div class="peek">
                <p>Test peek test peek<br/>Test peek</p>
                <h3>MORE TESTING</h3>
            </div>
        </div>
    </div>
</div>
<div class="main">
    <div>
        <div class="content">
            <h1>Title</h1>
            <h2>Subtitle</h2>
            <div class="peek">
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <h3>MORE<br/>Peek</h3>
            </div>
        </div>
    </div>
</div>
<div class="main large">
    <div>
        <div class="content">
            <h1>Title</h1>
            <h2>Subtitle</h2>
            <div class="peek">
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <p>Description<br/>with<br/>many<br/>lines.</p>
                <p>Description<br/>with<br/>many<br/>lines.</p>
            </div>
        </div>
    </div>
</div>

Include a .main.effect .peek class for each main element with the same functionality as .main:hover .peek, and then assign an effect class to every main div that becomes visible during scrolling.

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

Tips for resolving the issue: React is unable to recognize the X prop on a DOM element

I have been experimenting with a library known as react-firebase-js for managing firebase authentication. However, my grasp of react and the concept of provider-consumer is somewhat limited. Initially, I created a large JSX construct at the top level, whi ...

JavaScript hard-coded object functions as an argument, however it does not reference the global object

Recently, I encountered a strange issue while working with JQuery Flot. Initially, my code looked like this: var plot = null; function initPlot () { plot = $.plot("#graph", myData, { /* my options here */ }); } and everything was functioning correc ...

What is the best way to use AJAX to load a PHP file as a part

I'm exploring different methods for making an AJAX call with an included file. Let's create a simple working example. Initially, I have my main index.php file which contains the following content. In this file, I aim to access all the data retur ...

What is the process for logging in using a Bootstrap modal?

I am currently working on creating a login feature using a bootstrap modal, but unfortunately, I am facing some issues with it. If anyone is willing to offer their assistance, I would greatly appreciate it. My knowledge of JavaScript and Bootstrap is limi ...

Next.js 13 app directory experiences 404 Not Found error due to dynamic routing issues

I recently built a straightforward to-do app using Next.js 13 paired with TypeScript. The process involved creating an array of objects, each comprising an id string and a name string. Subsequently, I iterated through the list and showcased the names withi ...

three.js LambertMeshMaterial

Hello everyone: I've noticed that my models using MeshLambertMaterial are consuming approximately 1.6GB of memory, exceeding the capabilities of a 32-bit Chrome browser. Switching to Basic Material drops the memory consumption to around 456mb, but the ...

Creating a customizable navigation bar using only CSS

I'm currently working on creating a navigation bar using only HTML and CSS, without the need for JavaScript to open and close it. However, I've encountered an issue where the navigation bar remains open even after setting display: none or visibi ...

Can the FB status update popup be displayed when clicked?

I've implemented a code on my website that allows users to update their Facebook status directly from the page: <head> <title>My Awesome Site</title> </head> <body> <div id="fb-root"></div> <script s ...

Updating content with jQuery based on radio button selection

Looking for assistance with a simple jQuery code that will display different content when different radio buttons are clicked. Check out the code here. This is the HTML code: <label class="radio inline"> <input id="up_radio" type="radio" n ...

Issue with jQuery ajax in Internet Explorer 6

Hey there, I'm dealing with a strange issue: The success handler in my $.ajax call looks like this: function(data){ alert(data); } Seems pretty straightforward, right? The problem I'm facing is that the data sent by the server is ALW ...

Increase the width in a leftward direction

I am looking to increase the width from right to left. <div dir="rtl" id="progress"> <dt id='dt'></dt> <dd id='dd'></dd> </div> The ultimate objective here is to have this progress bar progr ...

Is it possible to prevent the selected option from being available in other select lists using AngularJS?

Can anyone help me figure out how to disable an option in one select list when it is selected in another using AngularJS? I have set up a select list for From Year and To Year with ng-repeat, which is working fine. Now, I just need to implement the functio ...

The WordPress website showcases inline CSS code

I'm currently working on a project at and upon reviewing the source code, I noticed the following snippet. This inline code is part of the Theme, and I'm wondering if there's a way to extract this code into a separate stylesheet within the ...

"Upon clicking the login button, I encountered an issue with redirecting to the destination

I've been working on developing a login page using Angular framework. However, I'm facing an issue where I am unable to access the destination page after entering the login credentials. Below, you can find a snippet of the code from the login.com ...

How can I make a jQUery fadeIn effect triggered by the current URL

Imagine I provide a URL like example.php?mode=select&ID=1#age Can jQuery fadeIn be added based on the URL? This URL will be on a different page. When a user clicks on it, a page will load and the age field should fade in. If this is not possible, ar ...

Guide to aligning text vertically in a column alongside an image using Bootstrap 5

I have been using the Bootstrap 5.3.2 grid system for most of my website. I am trying to create a layout with an image on the left and text on the right. However, I am facing an issue with aligning the text vertically in the column. Is there a way to achie ...

Can you explain the distinction between the two methods of coding in jQuery?

1, $.each(a,f); 2,$("a").each(f); I understand the purpose of the last line, where it selects all <a> elements in the document and then calls the each() method to invoke function f for each selected element. However, I am unsure about the meani ...

The URL in an AJAX request includes a repeating fragment due to a variable being passed within it

const templatePath = envVars.rootTemplateFolder + $(this).attr('link'); console.log("templatePath : ", templatePath); $.ajax({ method: "GET", url: templatePath, context: this, success : function(result){ ...

Assistance needed with Chrome extension - seeking guidance on how to include the URL of the current page into the HTML of the default popup in the manifest.json file

I have a manifest file that looks like this: { "name":"Improve the Web extension", "version":"0.2", "manifest_version":2, "description":"Enhancing web accessibility is crucial. Poorly designed websites can exclude disabled individuals. By ...

Displaying customized local JSON information in a Tabulator Table

I'm trying to incorporate local JSON data into a table using Tabulator. Specifically, I want to display the element obj.File.Name from the JSON. While I can render the data in a regular table, I face issues when trying with Tabulator as the data does ...