最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Change image size on JavaScript event - Stack Overflow

matteradmin7PV0评论

I am using a template that I bought offline that uses JavaScript. I am not that great with JavaScript and I would like to make it so that the logo goes from being small to larger as soon as the user clicks on one of the links above example:

Before:

After:

As I said before I am not good at JavaScript, I am fairly good at HTML and CSS, but stil a little grey on advanced CSS. Is there a way I can do this on page change. The code for this is all one single page and does not reload from server so I can't set an event via "page load". The loading is done via JavaScript. For a live demo you can go here: Logo Change Site

I am using a template that I bought offline that uses JavaScript. I am not that great with JavaScript and I would like to make it so that the logo goes from being small to larger as soon as the user clicks on one of the links above example:

Before:

After:

As I said before I am not good at JavaScript, I am fairly good at HTML and CSS, but stil a little grey on advanced CSS. Is there a way I can do this on page change. The code for this is all one single page and does not reload from server so I can't set an event via "page load". The loading is done via JavaScript. For a live demo you can go here: Logo Change Site

Share Improve this question edited Feb 24, 2022 at 22:27 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 6, 2013 at 19:27 user1574685user1574685 1811 gold badge2 silver badges11 bronze badges 1
  • 1 This would be a good application for jQuery's animate function. – nullability Commented May 6, 2013 at 19:48
Add a ment  | 

3 Answers 3

Reset to default 1

Give the logo <img /> an id, such as:

<img id="siteLogo" src="images/logo.png" alt="" height="200">

Add a <script> tag, such as this:

<script type="text/javascript">
    var img = document.getElementById("siteLogo");
    img.addEventListener('click', function () {
        if (img.height != 200) {
            img.height = 200;
        } else {
            img.height = -1;
        }
    });
</script>

The above will cause the logo to toggle between big and small when clicked.

You would then have to play around with the <header> to not have a fixed height; this'll make it resize when the logo changes size.

you can use some jquery to change the property of your image. This can be done with some ajax :

jquery(function () {

    $("#your image id").click(function () {

        //Then you can change the style properties of your image, this is how you can do it.

        $("#image id you want to modify").attr("style","...");

        //the ... in the ", this is there you will edit your style.

    });

});

So this is a way you can do what you want to do, but you have to know that you will have to link a jquery librairie if you want to use jquery. There is documentation about jquery.

Edit: What you can do too, is to create an external js file that you can call ajax.0-1.js or whatever, and then link this file to your html page.

I see you load jQuery on your page, that's good because it makes this task simple ;)

just before </body>

<script type="text/javascript">
    $(document).ready(function(){
        $('.navigation li').on('click', function(){
                $('#img-logo').css('width','212px');
                $('#img-logo').css('height','70px');
        });
    });
</script>

then change your html:

<img src="http://yakko.cs.wmich.edu/~brain/SAS/images/logo.png" alt="logo"  id="img-logo"/>

you can find jsfiddle here:
http://jsfiddle/arqjn/


you can add some effects on css-transition if u want
make sure to use a good image (double-sized on normal view) or you will see blur on zoom

Post a comment

comment list (0)

  1. No comments so far