如何在页面上向下滚动时调整图像大小?

时间:2022-08-24 16:49:44

I am creating a nav bar for a website, however I can't seem to get this to work. I want the logo to shrink as you scroll down on the site. I've tried the webkit animation stuff, and a few javascript/jQuery functions but they don't want to cooperate. This is the current function i've played with and it doesn't seem to like it. How do I fix it?

我正在为一个网站创建一个导航栏,但我似乎无法让它工作。当您在网站上向下滚动时,我希望徽标缩小。我已经尝试了webkit动画的东西,以及一些javascript / jQuery函数,但他们不想合作。这是我玩过的当前功能,它似乎不喜欢它。我如何解决它?

HTML:

<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>

CSS:

.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;
}

2 个解决方案

#1


0  

You could do something like this with jQuery:

你可以用jQuery做这样的事情:

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

Full example here: https://jsfiddle.net/sL89qxcx/

这里有完整的例子:https://jsfiddle.net/sL89qxcx/

#2


0  

you could use onscroll event :

你可以使用onscroll事件:

<body onscroll="myFunction()">

#1


0  

You could do something like this with jQuery:

你可以用jQuery做这样的事情:

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

Full example here: https://jsfiddle.net/sL89qxcx/

这里有完整的例子:https://jsfiddle.net/sL89qxcx/

#2


0  

you could use onscroll event :

你可以使用onscroll事件:

<body onscroll="myFunction()">