Shifting image from center to corner as you scroll

Hello there, I'm new to this so please bear with me for any obvious mistakes, thank you :)

My goal is to have an image move from the center of the screen to the corner of the screen as you start scrolling. I've made some progress on this, but I've encountered an issue. When I scroll back to the top, the image doesn't return to its original position.

Appreciate any help in advance :)

 window.onscroll = function() {scrollFunction()};
    
    function scrollFunction() {
      if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
        document.getElementById("logo").style.width = "70px";
        document.getElementById("logo").style.margin = "0px";
        document.getElementById("logo").style.position = "fixed";
      } else {
        document.getElementById("logo").style.width = "30%";
        document.getElementById("logo").style.margin = "0 auto";
        document.getElementById("logo").style.position = "relative";
      }
    }
#logo {
    width: 30%;
    transition: 0.5s;
    margin: 0 auto;
    margin-top: 15%;
    }
    
    
    .scroll {
    height: 1200px;
    }
<!Doctype html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://use.typekit.net/siu5pjx.css">
    </head>     
<body>
    
    <!--LOGO-->
    <div id="logo">
    <a href="<?php echo get_page_link( get_page_by_title( home )->ID ); ?>">
    <img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" width="100%">
    </a>
    </div>
    
    <div class="scroll"></div>  
</body>
</html>

Answer №1

When scrolling back up, the issue arises where the margin is not being included.

A simple solution to this problem is to re-add the margin using JavaScript.

window.onscroll = function() {scrollFunction()};
    
    function scrollFunction() {
      if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
        document.getElementById("logo").style.width = "70px";
        document.getElementById("logo").style.margin = "0px";
        document.getElementById("logo").style.position = "fixed";
      } else {
        document.getElementById("logo").style.width = "30%";
        document.getElementById("logo").style.margin = "0 auto";
        document.getElementById("logo").style.marginTop = "15%";
        document.getElementById("logo").style.position = "relative";
      }
    }
#logo {
    width: 30%;
    transition: 0.5s;
    margin: 0 auto;
    margin-top: 15%;
    }
    
    
    .scroll {
    height: 1200px;
    }
<!Doctype html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://use.typekit.net/siu5pjx.css">
    </head>     
<body>
    
    <!--LOGO-->
    <div id="logo">
    <a href="<?php echo get_page_link( get_page_by_title( home )->ID ); ?>">
    <img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" width="100%">
    </a>
    </div>
    
    <div class="scroll"></div>  
</body>
</html>

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

Creating a JavaScript regular expression for formatting time input without using any external plugin

As I work on implementing a time input mask with the meridian of am|pm without using an input mask plugin, I've encountered some challenges. The input should not allow the user to start with alphabets, but my current pattern, although it works, clears ...

Invoking Ajax within a for loop

for (var i = 0; i < 5; i++) { using (x = new XMLHttpRequest()) sendRequest("GET","d.php?id=" + i), checkResponse(null), updateStatus = function() { if (x.state == 4 && x.responseCode == 200) notifyUser(i); } } My goal now is to e ...

Intercepting HTTP responses to handle headers

I'm facing an issue where I am trying to retrieve a custom header sent from the server in my $http response interceptor. However, the only header that is accessible is the Content-type header. How can I troubleshoot this problem? Here is part of my $ ...

Utilizing AngularJS to Retrieve Row Information Upon Button Selection

I'm currently developing an application that includes a feature allowing users to run a query from a drop-down menu and view the data in a table. Each row in the table will have a button, and when clicked, I need to capture the values of that specific ...

Solving Mixed Content Issues in JavaScript

I'm attempting to retrieve information from OMDB API, but I'm encountering an issue with mixed content images. OMDB pulls its data from IMDB, which does not allow the use of https images. Therefore, all image sources must be prefixed with http. ...

Having difficulty identifying the same element during testing

Here is the code snippet I'm working with, which checks for expected text: console.log(typeof browser.getText('.modal.modal--primary.pin-container h1')); expect(browser.getText('.modal.modal--primary.pin-container h1')).toContain( ...

Html elements remain stationary and do not shift positions

I am attempting to customize a web page by repositioning an input text element. Below is the code I'm using: <body> <h1>Shopping List</h1> <input id="search" type="search"> <input id='newItem' type="t ...

Generating a JavaScript object based on an array of keys

I'm currently grappling with a task that is proving to be quite challenging. I have a set of arrays structured like this: ['key1', 'key2', 'key3'] ['key1', 'key2', 'key4'] ['key1', ...

What could be causing an unidentified term to be included in the Vue SCSS compilation process

In my Vue application with TypeScript, I encountered an error during compilation that reads as follows: Failed to compile. ./src/style.scss (C:/../node_modules/css-loader/dist/cjs.js??ref--8-oneOf-1-1!C:/.../node_modules/vue-loader/lib/loaders/stylePostL ...

react component fails to rerender upon state change

I am struggling with a React functional component that includes a file input. Despite selecting a file, the text in the h1 tag does not change from choose file to test. The handleChange function is triggered successfully. A console.log statement confirm ...

Guide on integrating web api on android with javascript, jquery, and jsonp

I am attempting to create a button in my Android application that, when clicked, retrieves information from a web API and displays the results on my screen. Here is the code that performs the necessary tasks, but I need to integrate it into my Android pag ...

halt execution of npm test and erase any printed content

When I run npm test on my React project, it runs unit tests using jest and react testing library. The test logs (including console log lines added for debugging) are printed to the screen but get deleted after running the tests. It seems like the logs are ...

Creating a proxy for an HTTP video stream (from VLC) using NodeJS and ExpressJS

Using VLC 2.2.1, I am able to create an HTTP stream of my webcam from a computer named server. If I open VLC on another computer, client, and enter the network stream http://server:8080, the webcam video displays perfectly. A Wireshark capture of the HTT ...

Using $state outside of the AngularJS environment

Currently, I am working on an AngularJS application that is meant to be a hybrid mobile app for both android and iOS platforms. Within the project, there is a JavaScript file that does not belong to any module. In this particular JavaScript file, I need to ...

I'm having trouble getting the aggregation of a child collection to work properly in MongoDB when attempting to apply the $count

Could someone help me troubleshoot my aggregate query? I'm trying to sum the count values for each beacon, but it keeps returning 0. Please let me know if you spot any mistakes in the query. Sample Data [ { ...

Can data be retrieved from an Excel or CSV file using only an HTML5 Local database, without relying on Mysql?

Here's a scenario: I currently have an HTML form displayed on a webpage. My goal is to gather all the information submitted by users through this form and organize it into an Excel or CSV sheet. Is this achievable using only HTML 5, or will I need a ...

What's the key ingredient I need to add for a dropdown navigation menu?

What am I overlooking in my HTML and CSS coding to create a standard list dropdown in my navigation menu? When I preview this section of the page in Chrome and IE, the navigation area appears fine until I hover over the Fishing and Guides link. Instead of ...

Dynamic field refreshed on server side upon second button press

I'm encountering an issue where a hidden field that I update via Javascript only reflects the new value after clicking a button twice. Surprisingly, I can view the updated hidden field value when inspecting it through the browser. Default.aspx <s ...

Error message in Node v12: "The defined module is not properly exported."

When trying to export a function in my index.js file, I encountered an error while running node index.js: module.exports = { ^ ReferenceError: module is not defined Is there a different method for exporting in Node version 12? ...

Tips on leaving comments inside a render <div>

Hey there! I'm facing an unusual issue in my React+Webpack project. Even though I marked a line as a comment using //, it still gets rendered in the HTML output. Strangely, I have never encountered this problem before and can't figure out what tr ...