What is the best way to dynamically adjust the top CSS of an element when it is positioned at the top or

Is there a way to dynamically set the top CSS when an element is over the top or bottom of a page?

First, hover over the first cat image. It's working fine.

http://image.ohozaa.com/i/480/7YpWei.jpg

When I scroll the page to the bottom and hover over the first cat image again,

http://image.ohozaa.com/i/5b5/l2l3dj.jpg

The big cat image is now over the top of the page.

And when I scroll the page to the top and hover over the 3rd cat image again,

http://image.ohozaa.com/i/5db/ezZE1b.jpg

The big cat image is now over the bottom of the page.

How can I change the top CSS of the mouse-over effect element so that it does not cover the top or bottom of the page?

http://jsfiddle.net/L7Lyjtp7/

index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<style type="text/css">
<?PHP
    for($i=0;$i<3;$i++)
    {
?>
        .top_box<?PHP echo $i; ?>{
        left: 99px;
        top: 0px;
        position: absolute;
        border: 1px solid #000;
display: none;
        }
<?PHP
    }
?>
</style>

<?PHP
    for($i=0;$i<3;$i++)
{
?>
<script type="text/javascript">
$(document).ready(function(){    
    $("#thumbnail<?PHP echo $i ; ?>").hover(
        function(){
            var id = $(this).data("id");
            if(id!==undefined){
             $("#" + id).show();   
            }
            $("#top_box<?PHP echo $i; ?>").show(); 

        },function(){
            $("#top_box<?PHP echo $i; ?>").hide(); 

        });
});
</script>
<?PHP
    }
?>
</head>

<br><br><br><br><br><br><br><br><br><br>
<div style=" float: left; position: relative; width: 100%; margin-bottom: 200px; " >
    <div id="top_box0" class="top_box0">
        <img src="http://consciouscat.net/wp-content/uploads/2010/02/cat-licks-lips-200px.jpg">
    </div>
    <span id="thumbnail0" style=" margin: 7px; position: relative;">
        <img border="0" src="http://consciouscat.net/wp-content/uploads/2010/02/cat-licks-lips-200px.jpg" width="80" height="80"/>                
    </span>
</div>
<br>
<div style=" float: left; position: relative; width: 100%; margin-bottom: 200px; " >
    <div id="top_box1" class="top_box1">
        <img src="http://cat-bounce.com/cb.png">
    </div>
    <span id="thumbnail1" style=" margin: 7px; position: relative; ">
        <img border="0" src="http://cat-bounce.com/cb.png" width="80" height="80"/>                  
    </span>
</div>
<br>
<div style=" float: left; position: relative; width: 100%; margin-bottom: 200px; " >
    <div id="top_box2" class="top_box2">
        <img src="http://media.yopi.co.th/prod_pics/12/e/12827.jpg">
    </div>
    <span id="thumbnail2" style=" margin: 7px; position: relative;">
        <img border="0" src="http://media.yopi.co.th/prod_pics/12/e/12827.jpg" width="80" height="80"/>                  
    </span>
</div>

Answer №1

To achieve a fixed CSS layout, you can set it up this way: JSFIDDLE If you prefer the images to have cats beside them, you can incorporate jQuery to determine if the image is at the "top page"/"bottom page" and adjust the position as necessary.

.top_box0{
    left: 99px;
    top: 0px;
    position: fixed;
    border: 1px solid #000;
    display: none;
}
.top_box1{
    left: 99px;
    top: 0px;
    position: fixed;
    border: 1px solid #000;
    display: none;
}
.top_box2{
    left: 99px;
    top: 0px;
    position: fixed;
    border: 1px solid #000;
    display: none;
}

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

Unexpected output from Material UI Textfield

When attempting to print a page of my React app using window.print(), everything prints correctly except for the Textfield component from Material UI. It works fine when there are only a few lines of text, but when there is a lot of text, it appears like t ...

Using Leaflet to beautify categorical json information

As a beginner in coding, I must apologize if a similar question has already been asked. I've spent days searching but haven't found the right combination of terms to find examples for my scenario. I am exploring various small use cases of differ ...

Understanding Node.js document object

Hey, I'm currently trying to execute a JavaScript function on the server side using node.js, but I've encountered an issue. The function relies on accessing hidden values within the returned HTML using the document DOM, however, the document obje ...

Tips for displaying videos with sound when the autoplay has been successfully implemented, but the audio is not working

I've been working on creating a video player with autoplay functionality, but I'm facing an issue with the sound not playing. Here's what I currently have in my code, but it's not behaving as expected: var myaudio = docum ...

Problem with Google's PageSpeed Insights - Focus on Making Most Important Content Visible

During the process of creating a comprehensive website for a client who has a strong affinity towards Google tools and recommendations, I have encountered an interesting challenge: Despite my best efforts, I seem unable to attain a flawless score for the ...

Create a "load additional data" button

I'm working on building my blog and I've run into a roadblock while trying to implement a load more button. Here's what I currently have: actions: { LOAD_ARTICLE_LIST: function ({ commit }) { axios.get('someurl/articles') ...

Generate a hyperlink that directs to a page and triggers the expansion of an accordion section

I am currently working with a Bootstrap 5 accordion component: <div class="accordion" id="accordionIndexPage"> <div class="accordion-item"> <h2 class="accordion-header" id="flush-headingOne& ...

Exploring the world of jQuery waypoints and the art of modifying

This is only the second question I'm asking here, so please be gentle! I've been experimenting with jQuery waypoints to dynamically show and hide a border under my navigation menu based on scroll position. For instance, when the sticky nav is ov ...

Styling div elements to match the dimensions of table rows in CSS

When it comes to CSS, I wouldn't call myself an expert; My question is: is there a way for a div tag to inherit dimensions from specific table rows based on their class or id? For instance: Imagine we have a table with multiple rows, but we don&apos ...

There was an error in reading the property 'RNFSFileTypeRegular' of null, with the JavaScript engine reporting as hermes

Below are the dependencies listed in my package.json file: "dependencies": { "@expo/vector-icons": "^14.0.2", "@react-navigation/native": "^6.0.2", "expo": "~51.0.23", & ...

Fixing the height of the body padding with JS or JQuery for a

I have a rather straightforward question that pertains to an issue I am facing while building a website using BS4. Specifically, my problem revolves around a fixed-top navbar with the class "fixed-top". The challenge stems from not knowing the actual heig ...

Creating custom shaders for YouTube videos within a Three.js userscript

I want to add a shader effect to YouTube videos. My current approach involves using Three.js to implement a shader on a video. Specifically, I am trying to adapt this example of applying a shader to a video (code available here) into a Tampermonkey usersc ...

Error: A SyntaxError was encountered due to a missing closing parenthesis after an argument list while writing JavaScript within PHP code

I'm facing an issue writing JavaScript within PHP code. Here's my script : echo ' <script>'; echo ' $(function(){'; echo ' x = parseInt($("#counter2").val());'; echo ' $("#add_row2").click(function(){&apo ...

Chrome browser not responding to jQuery .html refresh

I am facing an issue with a page containing a list of tasks, each with a dropdown menu featuring a list of teams to which the task can be assigned. However, pre-populating the dropdown in the Action function is not feasible due to the large number of tasks ...

What is the best way to reference an ImageButton using jquery?

I created an HTML page with a special div in the body that contains a collection of buttons and images. <div id="div_one"> <button id="button_one"> <asp:Image id="image_button_one" ImageUrl=".." runat="server"> </button> </div& ...

Is there a way to automatically convert English numbers to Persian/Arabic as soon as a user types them into the input tag?

Currently, I am working with Vuejs and trying to hide the user's characters in an input tag and replace them with my own characters (specifically numbers). I have attempted using @onchange, Watch, as well as getters and setters within computed proper ...

Uploading profile images with AngularJS and Firebase

I am encountering an issue with my JavaScript image uploader. It successfully uploads an image and provides the image src, but I need to save this value in my FireBase DB for the current user that is being created. Initially, I attempted to output the img ...

What could be the reason for the CSS stylesheet not functioning properly on mobile devices?

I am a newcomer to web development and currently working on a website for a massage therapist using bootstrap. However, I am facing an issue where my CSS styles are not displaying correctly on mobile devices. The backgrounds and bootstrap navbar disappear, ...

Troubleshooting Tips: Investigating a Service Call in AngularJS 2 using ES6 Syntax

MY DILEMMA: I have recently started learning Angular2 and found myself wanting to debug a service call. The service appears to have been properly called, as evidenced by the view display. However, when trying to log the content of the variable holding t ...

Retrieving ID of an element to be animated with jQuery

I have a sprite image that changes background position when hovered over, and while it's currently working, I'm looking for a more efficient way to achieve this. I need to apply this effect to several images and am exploring ways to avoid duplica ...