Is there a way for me to determine if an image is at the top of the screen?

As I scroll, I am looking for a way to hide my menu and trigger other actions when an image reaches the top of the screen. These images span the full width of the page, so they pass through every point on the x-axis. I've attempted using elementFromPoint, but that only gives me the entire html document. Is there a different method I can use to achieve this?

Answer №1

If your image remains fixed in place (which seems to be the case), you can utilize the following approach:

    var timer;
    $(document).scroll(function () {
        // Implementing a timeout to prevent
        // continuous execution at each scroll event.
        // Only triggers when scrolling has stopped
        if(timer) clearTimeout(timer);
        timer = setTimeout(function () {
            if($('#hello').offset().top - $(window).scrollTop() <= 0) {
                $('#hello').attr('alt', 'Im touching the top!');
                $('#status').text('Image is touching the top, or beyond it');
            } else {
              $('#hello').attr('alt', 'Not at top');
              $('#status').text('Image is not touching the top');
            }
        }, 100);
    });
html, body {
    height: 400%;
}
img {
    position: absolute;
    top: 150px;
    width: 100px;
    height: 100px;
}
#status {
    position: fixed;
    top: 0;
    right: 0;
    background: #000;
    color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="status">Image is not touching the top</div>
<img id="hello" alt="I'm an image">

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

Messaging through Express.js via whatsapp-web.js is not working on the server

After successfully confirming the QR code with my phone, the console displays "READY," but I encounter a "404" error when sending a JSON request to the "/sendmessage" endpoint. The same issue arises when accessing the "/getqr" endpoint. Despite referencing ...

Displaying an ASP.NET chart within a jQuery dialog box

I am attempting to create a page with small charts (using asp control objects) that enlarge into a styled jQuery modal/dialog when clicked. Currently, I am facing an issue where the modal appears blank. It seems like my code behind function 'Enlarge&a ...

Reviewing packages often reveals a multitude of mistakes

I attempted to address some issues in my project by executing npm audit fix Unfortunately, this did not yield significant results. However, when I added the --force flag, the outcome was even worse than before: fix available via `npm audit fix` 10 vulner ...

Connecting nodes to edges based on their unique ids in the d3.js graph library

I am encountering an issue with this code while trying to integrate it with a new JSON object called 'new_json'. Specifically, I need the links in this code to be created based on the nodes' IDs. Can anyone provide assistance with this? va ...

After closing the modal, I am attempting to reset it, but unfortunately, it is not functioning as expected

I have a modal box with the following structure: <div class="modal-coupon-code"> <center class="show-code"><button type="button" class="btn btn-warning" data-toggle="modal" data-target="#couponcode<?php echo $result->id; ?> ...

serializeArray encounters difficulty in locating certain input elements

I've created this HTML layout: <div class="col-md-12"> <div class="form-group"> <label>Mini description (displaying the latest added destinations and meta description)</label> <textarea class="for ...

Error Received While Attempting to Log in using Ajax

Having an issue with logging in using ajax and php. I am able to log in successfully, but when trying to display an alert message and refresh the page upon login, it gives me an error without refreshing. However, upon manually refreshing the page, I can se ...

The extent of the modal window (AngularJS directive)

I've been experimenting with a modal window feature using Angular UI Bootstrap. Within the parent controller, I've defined the following function: $scope.open = function () { var modalInstance = $modal.open({ templateUr ...

Error message: Node: TypeError - Attempted to access the 'sort' property of an undefined variable while using findOneAndUpdate() within a find() operation

My code snippet is shown below: var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost:27017/school', function(err, db) { if (err) throw err; db.collection('students').find().toArray( ...

Unexpectedly, Ajax call is triggering additional callbacks

I am currently facing an issue with my AJAX request in the code below. The Chrome Inspector is showing that the callback function associated with the request is being called twice, resulting in the response being logged into the console twice. Additional ...

Refresh the page to verify if the user has successfully established a connection with PhoneGap through AngularJS

I am currently developing an app using PhoneGap. I have successfully implemented a feature to check if the user is connected to the internet or not. However, if the user is not connected, I would like to provide a button for them to click on in order to re ...

I am currently facing a challenge in React Highcharts where I am unable to remove and redraw the graph easily

Having an issue where I can't remove and redraw the chart in my React Highchart project. I've been unable to find a solution for this problem. Here is the code snippet: import { useState, useEffect, useRef } from "react"; import Highch ...

Tips for ensuring that a nested object in an array contains only a single object

My array is structured like this: [ { object1:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1} }, { object2:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1} }, { object3:{ childObj1:[grandChild1,grandChild2 ...

What is the correct way to display single line code snippets in Firefox?

When attempting to display a single line of code within a code block, the layout appears too close to the line numbers (this issue only occurs in Firefox): https://i.sstatic.net/WXZp7.png The HTML code responsible for this display is automatically generat ...

Unusual actions when making a $.ajax call using the PUT method

When making a call to $.ajax, I use the following code: $.ajax({ type: 'PUT', url: model.url(), data: {task: {assigned_to: selected()}}, contentType: 'application/json' }) The function selected() returns an array. However, th ...

Utilize JQuery AJAX to retrieve a PHP array and then store it in a JavaScript variable

I am facing a challenge that seems simple to resolve, but it's proving to be quite tricky. Currently, I have a functional PHP file that fetches data from a database and stores it in an array. This file does not require any parameters and the output i ...

Tagging photos - Displaying tagged box when hovering over a name

I am currently working on a feature where hovering over a tag name in the list will display a box at the tagged location (similar to Facebook). Below is the code I have: Viewtag.php <?php $sql = "SELECT * FROM image_tag ORDER BY `pic_id`"; $qry = mys ...

Generate a download link for the image being shown in a div element

I am working on a project where I need to display HTML content offline within a Windows Application. The application, developed in Autoplay Media Studio, includes an iexplore box to load the HTML. Before the application loads, I have set up a silent insta ...

Selecting CSS Properties with jQuery

I am trying to make an image change color to blue and also change the background color to blue when it is clicked inside a div. However, my code doesn't seem to be working as expected. Is there a more efficient way to apply CSS to elements? FIDDLE v ...

Utilize a function on specific attribute values

I have a function in JavaScript that is designed to convert all relative URLs into absolute URLs. function rel2Abs(_relPath, _base); //_relPath represents the relative path //_base indicates the base URL Now, my objective is to implement this function on ...