What is the best way to adjust the size of an image as I navigate down a webpage?

I've been working on designing a navigation bar for a website, but I'm running into some issues. My goal is to have the logo shrink as the user scrolls down the site. I've experimented with webkit animations and various javascript/jQuery functions, but nothing seems to be working properly. The current function I've tried doesn't seem to be cooperating. How can I go about fixing this?

Here is the HTML code I've been playing around with:

<html>
    <body>
        <script>
            $(document).on("scroll", function() {
            if($(document).scrollTop() >= 1)
            {
                $(".nav .logo img").css('-webkit-transform', 'scale(.5, .5');
                $(".nav .logo img").css('-ms-transform', 'scale(.5, .5');
                $(".nav .logo img").css('transform', 'scale(.5, .5');
            }
            else 
            {
                $(".nav .logo img").css('-webkit-transform', 'scale(1, 1');
                $(".nav .logo img").css('-ms-transform', 'scale(1, 1');
                $(".nav .logo img").css('transform', 'scale(1, 1');
            }
        });​
        </script>
        <div class="nav">
            <div class = "logo">
                <a href = "index.html"><img src="Pics/siren.png" alt="" width="196" height="196"/></a>
            </div>
        </div>
    </body>
</html>

And here is the CSS code for styling the navigation elements:

.nav{
    position: fixed;       
    top: 0;
    z-index: 1;
    width: 100%;
    height: 100px;
}
.nav .logo{
    position: fixed;
    text-align: left;
    z-index: 2;
    top: 0;
    bottom: 100px;
    overflow: hidden;
    opacity: .5;
}

Answer №1

Here is an example of using jQuery to adjust the width of an image based on scroll position:

$(document).on('scroll', function() {
  if ($(document).scrollTop() >= 10) {
    $('.logo img').css('width', '50px');
  } else {
    $('.logo img').css('width', '');
  }
});

You can view the full code and demo here: https://jsfiddle.net/abcd1234/

Answer №2

one option is to utilize the onscroll event :

<body onscroll="myFunction()">

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

What is the best way to clear the content of a contenteditable element in React?

I have a scenario where I am rendering an array of items, each with a contenteditable field. MainComponent.js import { useState } from "react"; import Item from "./Item"; import "./styles.css"; export default function MainC ...

Using jQuery to position an element above an ID

I'm currently working on a unique portfolio gallery for my website, but I've encountered a problem. What I'm aiming for is to create a gallery that will slide up when a thumbnail is clicked, without loading all images on the page at once. T ...

I have been attempting to extract real estate listings from an MLS website using Beautiful Soup and have found some success. I am now considering if using Selenium would be a more efficient option

Perhaps this question has been answered before, which could help guide me in the right direction. The website loads 24 listings at a time and then displays a "see more results" button to load the next batch of 24 listings while keeping the initial 24 visib ...

The event.js file is displaying an error at line 141, causing an unhandled 'error' event to be thrown

Trying to execute node 4.2.2 on a Mac OS is causing me some confusion as I keep encountering this error message: events.js:141 throw er; // Unhandled 'error' event ^ Error: spawn /Users/user/Documents/Projects/project-x/node_modules/ ...

Updating elements within a subarray in JavaScript can easily be accomplished by accessing the

I need help updating nested array elements in JavaScript. I want to convert dates into a different format. How can I update the nested elements? array1 = [ { "week": [ "2019-05-06T16:00:00.000Z", "2019-05-07T16:00:00.000Z", "2019-05-08T16:00:00.000Z", "20 ...

Unable to pass data to Laravel 5.1 controller using Ajax Load function

Jquery Ajax Request var pageNumber = $(this).attr("data-page"); $("#results").load('pagination',{"page":pageNumber}, function(){ }); Route Configuration File Route::get('pagination',"MyController@pagination" ); Server-Side Contr ...

Transforming the font landscape with Twitter Bootstrap 3's Glyphicons

I have an unordered list with icons styled using Twitter Bootstrap 3. The Carme font from Google Fonts is used for normal text, and works well in that context. However, when I add icons to the unordered list using BS3, the font-family of that particular ...

Swapping out a button

I tried searching for a solution to my problem but I couldn't find it because of my limited English proficiency. Therefore, I apologize if my question has already been answered. https://i.sstatic.net/XhQBM.jpg I would like to place my button in the ...

What is the maximum storage capacity for variables on a NodeJS/ExpressJS server when handling multiple users?

As I delve into the node server code, I find myself pondering on the idea of storing temporary data - objects and arrays - within variables. These variables are housed in the client variable of a socket and are purged when connection with the client is l ...

Show inline-block elements within a div that has a variable width, allowing for horizontal scrolling

Currently, my layout is as follows: https://jsfiddle.net/0r6cdwht/2/ Is there a way to enable horizontal scrolling when the items displayed inside the div overflow? I've done some research and came across a solution that suggested using white-spac ...

Discover a multitude of items simultaneously using mongoose

I am trying to find multiple objects in a mongo model with different properties simultaneously. model.find({uuid: 235q422462}, {uuid: 435q4235239}, function(err, objects){ if(err){ console.log(err) } else { console.log(objects) ...

"Activate the parent window by navigating using the accesskey assigned to the href

I have integrated a bank calculator tool into a website. The calculator opens in a new window, but I am encountering an issue. Users need a shortcut to open the calculator multiple times. I have discovered the accesskey feature, which works the first tim ...

Arranging elements within the flex container in perfect alignment

I'm stuck trying to figure out how to adjust the layout of a flex container with three child elements. Here is the code I'm working with: html,body,div {margin:0;padding:0;} .cont { display: flex; justify-content: space-between; height: ...

The functionality of checking all checkboxes is not functioning properly within a custom function

function toggleTreeView(id) { $("#TreeUL" + id).slideToggle(function() { if ($(this).is(':hidden')) { $("#treeCollapse" + id).addClass("glyphicon-plus"); $("#treeCollapse" + id).removeClass("glyphicon-minus"); } else { ...

Tips on expanding the dimensions and incorporating more members in a radar graph's Chartjs tag

I need to make some adjustments to the font size and color in a radar chart. Specifically, I want to change the name on the side of each data point. I have already tried adjusting the legend labels using the following code: options={{ ...

"Mastering the art of debouncing in Angular using

I am facing an issue where, during a slow internet connection, users can press the save button multiple times resulting in saving multiple sets of data. This problem doesn't occur when working locally, but it does happen on our staging environment. E ...

JavaScript code must be tailored to reference a JS file based on the specific environment, whether it be Production, Development, or staging

I need to determine which .js file to refer based on the URL of Prod, Dev, and QA environments. For Production URLs (domain.com and www.domain.com), I should refer to prod.js file. For Dev and QA URLs (dev.domain.com and staging.com), I should refer to s ...

Filtering controls within a table are not displayed in VueJS

I have been attempting to implement date filtering in my data table based on a demo I followed. Despite meeting the filter requirements, no results are being displayed which is perplexing. Below are the imports and data from the file containing the table: ...

Step-by-step guide on displaying elements within an array of objects

Upon receiving an object from a website, I discovered that it includes an array of objects along with other parameters. When I use console.log(req.body.cart), the output is [ { title: 'iphone 6', cost: '650' } ], but I only require the ...

How can you use a jQuery Selector to target all images and sources within dynamic containers?

I am currently attempting to identify all images and their paths (srcs) within the dynamic containers. The dynamic containers refer to container patterns stored in an array, which is filled dynamically. For example: var containers = new Array( ...