最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to shrink an image width based on scroll position - Stack Overflow

programmeradmin0浏览0评论

I'm looking to shrink a logo based on scroll

So far, I have something like this

logoSize = function(){
    var headerOffset = $(window).height() - 650;
    var maxScrollDistance = 1300;
    $(window).scroll(function() {
        var percentage = maxScrollDistance / $(document).scrollTop();
        if (percentage <= headerOffset) {
            $('.logo').css('width', percentage * 64);
        }
        console.log(percentage);
    });
}

logoSize();

I'm close, but the image either starts too wide or it shrinks too quickly, I need it to happen for the first 650px of scroll as you can see - Any ideas? Perhaps a percentage width would be better?

I'm looking to shrink a logo based on scroll

So far, I have something like this

logoSize = function(){
    var headerOffset = $(window).height() - 650;
    var maxScrollDistance = 1300;
    $(window).scroll(function() {
        var percentage = maxScrollDistance / $(document).scrollTop();
        if (percentage <= headerOffset) {
            $('.logo').css('width', percentage * 64);
        }
        console.log(percentage);
    });
}

logoSize();

I'm close, but the image either starts too wide or it shrinks too quickly, I need it to happen for the first 650px of scroll as you can see - Any ideas? Perhaps a percentage width would be better?

Share Improve this question edited Dec 1, 2015 at 17:57 Callam asked Jan 12, 2015 at 16:58 CallamCallam 1,0191 gold badge15 silver badges24 bronze badges 1
  • 1 If it's happening too quickly, then animate to the changed width...not just change it. – Waxi Commented Jan 12, 2015 at 19:49
Add a ment  | 

1 Answer 1

Reset to default 6

I've re-written your code based on the assumption that you have a target size in mind , e.g. after scrolling 650px you want your image to be 250px wide.

It scrolls smoothly between the native size and the target size, and takes into account the fact that the window height could be less than your maximum scrolling distance:

logoSize = function () {
    // Get the real width of the logo image
    var theLogo = $("#thelogo");
    var newImage = new Image();
    newImage.src = theLogo.attr("src");
    var imgWidth = newImage.width;

    // distance over which zoom effect takes place
    var maxScrollDistance = 650;

    // set to window height if that is smaller
    maxScrollDistance = Math.min(maxScrollDistance, $(window).height());

    // width at maximum zoom out (i.e. when window has scrolled maxScrollDistance)
    var widthAtMax = 500;

    // calculate diff and how many pixels to zoom per pixel scrolled
    var widthDiff = imgWidth - widthAtMax;
    var pixelsPerScroll =(widthDiff / maxScrollDistance);

    $(window).scroll(function () {
        // the currently scrolled-to position - max-out at maxScrollDistance
        var scrollTopPos = Math.min($(document).scrollTop(), maxScrollDistance);

        // how many pixels to adjust by
        var scrollChangePx =  Math.floor(scrollTopPos * pixelsPerScroll);

        // calculate the new width
        var zoomedWidth = imgWidth - scrollChangePx;

        // set the width
        $('.logo').css('width', zoomedWidth);
    });
}

logoSize();

See http://jsfiddle/raad/woun56vk/ for a working example.

发布评论

评论列表(0)

  1. 暂无评论