Scale logo on scroll with jQuery, dependant on scroll percentage












0















I want to scale down logo in the header as user scrolling down the page.
When he hits a certain breakpoint, scaling should stop.
Also, scale can't be more or less specified hardcoded sizes.



Live case:
The header is separated into two section - top and bottom. The bottom part is sticky (pos: fixed) and the top part gets faded out (pos: absolute).
Logo should be smoothly scaled, so it's big (140px height) when all header elements are visible and small (50px height) when it's sticky



So, I can't figure out how to calculate a number within a range of limit[0] and limit[1] that is dependant on the percentage of scroll distance between 0 and 100 (100 is a header top height in my case)



Currently, it's scaling 1 to 0, but I need to scale 1 to 0.357



===



Short video (10s) to demonstrate a problem



https://monosnap.com/file/p5P7GMkn3BKKMu9glrnmBiO5X6951z



Codepen



https://codepen.io/dpmango/pen/EOXVww



===



$(window).on('scroll', function(e) { 
var vScroll = _window.scrollTop();
var $logo = $('[js-header-logo]');
var $top = $('[js-header-top]');
var topHeight = $top.outerHeight();
var logoLimits = [1, 0.357] // [140, 50] // scale factor

var scrollPercent = 1 - (vScroll / topHeight) // 1 -> 0
var calcedScale = scrollPercent // what is logic ?

// limit rules
if ( vScroll > topHeight ){
calcedScale = logoLimits[1]
}
if ( vScroll < 0 ){
calcedScale = logoLimits[0]
}

// set values to DOM
$logo.css({
"transform": 'scale('+ calcedScale +')'
})

});









share|improve this question

























  • Can you post the HTML?

    – Minder Mondo
    Nov 15 '18 at 19:32











  • @MinderMondo Thank you for the comment, but html is not a case here. It's a javascript question

    – Sergey Khmelevskoy
    Nov 15 '18 at 19:40






  • 1





    I believe what you're looking for is just a simple Linear Normalization formula. I'd written an answer a while back with a basic JS function that takes input bounds (0 and 100 in your case), the normalized bounds (0.357 and 1 in your case), and the number you'd like to convert (the current percent), to give you the new number. You can see that here.

    – Tyler Roper
    Nov 15 '18 at 20:13








  • 1





    And for some follow-up, I'd actually re-used that same formula in a much more recent answer where someone was trying to convert a test grade (0-100) to a GPA (1.0 - 4.0), if that implementation is more suitable to your specific example.

    – Tyler Roper
    Nov 15 '18 at 20:22






  • 2





    @TylerRoper Exactly what I was looking for, but unable to describe. I've added normalize(vScroll, topHeight, 0, logoLimits[1], logoLimits[0]) from your GPA example and it works! Thank you so much

    – Sergey Khmelevskoy
    Nov 15 '18 at 20:29


















0















I want to scale down logo in the header as user scrolling down the page.
When he hits a certain breakpoint, scaling should stop.
Also, scale can't be more or less specified hardcoded sizes.



Live case:
The header is separated into two section - top and bottom. The bottom part is sticky (pos: fixed) and the top part gets faded out (pos: absolute).
Logo should be smoothly scaled, so it's big (140px height) when all header elements are visible and small (50px height) when it's sticky



So, I can't figure out how to calculate a number within a range of limit[0] and limit[1] that is dependant on the percentage of scroll distance between 0 and 100 (100 is a header top height in my case)



Currently, it's scaling 1 to 0, but I need to scale 1 to 0.357



===



Short video (10s) to demonstrate a problem



https://monosnap.com/file/p5P7GMkn3BKKMu9glrnmBiO5X6951z



Codepen



https://codepen.io/dpmango/pen/EOXVww



===



$(window).on('scroll', function(e) { 
var vScroll = _window.scrollTop();
var $logo = $('[js-header-logo]');
var $top = $('[js-header-top]');
var topHeight = $top.outerHeight();
var logoLimits = [1, 0.357] // [140, 50] // scale factor

var scrollPercent = 1 - (vScroll / topHeight) // 1 -> 0
var calcedScale = scrollPercent // what is logic ?

// limit rules
if ( vScroll > topHeight ){
calcedScale = logoLimits[1]
}
if ( vScroll < 0 ){
calcedScale = logoLimits[0]
}

// set values to DOM
$logo.css({
"transform": 'scale('+ calcedScale +')'
})

});









share|improve this question

























  • Can you post the HTML?

    – Minder Mondo
    Nov 15 '18 at 19:32











  • @MinderMondo Thank you for the comment, but html is not a case here. It's a javascript question

    – Sergey Khmelevskoy
    Nov 15 '18 at 19:40






  • 1





    I believe what you're looking for is just a simple Linear Normalization formula. I'd written an answer a while back with a basic JS function that takes input bounds (0 and 100 in your case), the normalized bounds (0.357 and 1 in your case), and the number you'd like to convert (the current percent), to give you the new number. You can see that here.

    – Tyler Roper
    Nov 15 '18 at 20:13








  • 1





    And for some follow-up, I'd actually re-used that same formula in a much more recent answer where someone was trying to convert a test grade (0-100) to a GPA (1.0 - 4.0), if that implementation is more suitable to your specific example.

    – Tyler Roper
    Nov 15 '18 at 20:22






  • 2





    @TylerRoper Exactly what I was looking for, but unable to describe. I've added normalize(vScroll, topHeight, 0, logoLimits[1], logoLimits[0]) from your GPA example and it works! Thank you so much

    – Sergey Khmelevskoy
    Nov 15 '18 at 20:29
















0












0








0








I want to scale down logo in the header as user scrolling down the page.
When he hits a certain breakpoint, scaling should stop.
Also, scale can't be more or less specified hardcoded sizes.



Live case:
The header is separated into two section - top and bottom. The bottom part is sticky (pos: fixed) and the top part gets faded out (pos: absolute).
Logo should be smoothly scaled, so it's big (140px height) when all header elements are visible and small (50px height) when it's sticky



So, I can't figure out how to calculate a number within a range of limit[0] and limit[1] that is dependant on the percentage of scroll distance between 0 and 100 (100 is a header top height in my case)



Currently, it's scaling 1 to 0, but I need to scale 1 to 0.357



===



Short video (10s) to demonstrate a problem



https://monosnap.com/file/p5P7GMkn3BKKMu9glrnmBiO5X6951z



Codepen



https://codepen.io/dpmango/pen/EOXVww



===



$(window).on('scroll', function(e) { 
var vScroll = _window.scrollTop();
var $logo = $('[js-header-logo]');
var $top = $('[js-header-top]');
var topHeight = $top.outerHeight();
var logoLimits = [1, 0.357] // [140, 50] // scale factor

var scrollPercent = 1 - (vScroll / topHeight) // 1 -> 0
var calcedScale = scrollPercent // what is logic ?

// limit rules
if ( vScroll > topHeight ){
calcedScale = logoLimits[1]
}
if ( vScroll < 0 ){
calcedScale = logoLimits[0]
}

// set values to DOM
$logo.css({
"transform": 'scale('+ calcedScale +')'
})

});









share|improve this question
















I want to scale down logo in the header as user scrolling down the page.
When he hits a certain breakpoint, scaling should stop.
Also, scale can't be more or less specified hardcoded sizes.



Live case:
The header is separated into two section - top and bottom. The bottom part is sticky (pos: fixed) and the top part gets faded out (pos: absolute).
Logo should be smoothly scaled, so it's big (140px height) when all header elements are visible and small (50px height) when it's sticky



So, I can't figure out how to calculate a number within a range of limit[0] and limit[1] that is dependant on the percentage of scroll distance between 0 and 100 (100 is a header top height in my case)



Currently, it's scaling 1 to 0, but I need to scale 1 to 0.357



===



Short video (10s) to demonstrate a problem



https://monosnap.com/file/p5P7GMkn3BKKMu9glrnmBiO5X6951z



Codepen



https://codepen.io/dpmango/pen/EOXVww



===



$(window).on('scroll', function(e) { 
var vScroll = _window.scrollTop();
var $logo = $('[js-header-logo]');
var $top = $('[js-header-top]');
var topHeight = $top.outerHeight();
var logoLimits = [1, 0.357] // [140, 50] // scale factor

var scrollPercent = 1 - (vScroll / topHeight) // 1 -> 0
var calcedScale = scrollPercent // what is logic ?

// limit rules
if ( vScroll > topHeight ){
calcedScale = logoLimits[1]
}
if ( vScroll < 0 ){
calcedScale = logoLimits[0]
}

// set values to DOM
$logo.css({
"transform": 'scale('+ calcedScale +')'
})

});






javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:14







Sergey Khmelevskoy

















asked Nov 15 '18 at 19:30









Sergey KhmelevskoySergey Khmelevskoy

1,08221029




1,08221029













  • Can you post the HTML?

    – Minder Mondo
    Nov 15 '18 at 19:32











  • @MinderMondo Thank you for the comment, but html is not a case here. It's a javascript question

    – Sergey Khmelevskoy
    Nov 15 '18 at 19:40






  • 1





    I believe what you're looking for is just a simple Linear Normalization formula. I'd written an answer a while back with a basic JS function that takes input bounds (0 and 100 in your case), the normalized bounds (0.357 and 1 in your case), and the number you'd like to convert (the current percent), to give you the new number. You can see that here.

    – Tyler Roper
    Nov 15 '18 at 20:13








  • 1





    And for some follow-up, I'd actually re-used that same formula in a much more recent answer where someone was trying to convert a test grade (0-100) to a GPA (1.0 - 4.0), if that implementation is more suitable to your specific example.

    – Tyler Roper
    Nov 15 '18 at 20:22






  • 2





    @TylerRoper Exactly what I was looking for, but unable to describe. I've added normalize(vScroll, topHeight, 0, logoLimits[1], logoLimits[0]) from your GPA example and it works! Thank you so much

    – Sergey Khmelevskoy
    Nov 15 '18 at 20:29





















  • Can you post the HTML?

    – Minder Mondo
    Nov 15 '18 at 19:32











  • @MinderMondo Thank you for the comment, but html is not a case here. It's a javascript question

    – Sergey Khmelevskoy
    Nov 15 '18 at 19:40






  • 1





    I believe what you're looking for is just a simple Linear Normalization formula. I'd written an answer a while back with a basic JS function that takes input bounds (0 and 100 in your case), the normalized bounds (0.357 and 1 in your case), and the number you'd like to convert (the current percent), to give you the new number. You can see that here.

    – Tyler Roper
    Nov 15 '18 at 20:13








  • 1





    And for some follow-up, I'd actually re-used that same formula in a much more recent answer where someone was trying to convert a test grade (0-100) to a GPA (1.0 - 4.0), if that implementation is more suitable to your specific example.

    – Tyler Roper
    Nov 15 '18 at 20:22






  • 2





    @TylerRoper Exactly what I was looking for, but unable to describe. I've added normalize(vScroll, topHeight, 0, logoLimits[1], logoLimits[0]) from your GPA example and it works! Thank you so much

    – Sergey Khmelevskoy
    Nov 15 '18 at 20:29



















Can you post the HTML?

– Minder Mondo
Nov 15 '18 at 19:32





Can you post the HTML?

– Minder Mondo
Nov 15 '18 at 19:32













@MinderMondo Thank you for the comment, but html is not a case here. It's a javascript question

– Sergey Khmelevskoy
Nov 15 '18 at 19:40





@MinderMondo Thank you for the comment, but html is not a case here. It's a javascript question

– Sergey Khmelevskoy
Nov 15 '18 at 19:40




1




1





I believe what you're looking for is just a simple Linear Normalization formula. I'd written an answer a while back with a basic JS function that takes input bounds (0 and 100 in your case), the normalized bounds (0.357 and 1 in your case), and the number you'd like to convert (the current percent), to give you the new number. You can see that here.

– Tyler Roper
Nov 15 '18 at 20:13







I believe what you're looking for is just a simple Linear Normalization formula. I'd written an answer a while back with a basic JS function that takes input bounds (0 and 100 in your case), the normalized bounds (0.357 and 1 in your case), and the number you'd like to convert (the current percent), to give you the new number. You can see that here.

– Tyler Roper
Nov 15 '18 at 20:13






1




1





And for some follow-up, I'd actually re-used that same formula in a much more recent answer where someone was trying to convert a test grade (0-100) to a GPA (1.0 - 4.0), if that implementation is more suitable to your specific example.

– Tyler Roper
Nov 15 '18 at 20:22





And for some follow-up, I'd actually re-used that same formula in a much more recent answer where someone was trying to convert a test grade (0-100) to a GPA (1.0 - 4.0), if that implementation is more suitable to your specific example.

– Tyler Roper
Nov 15 '18 at 20:22




2




2





@TylerRoper Exactly what I was looking for, but unable to describe. I've added normalize(vScroll, topHeight, 0, logoLimits[1], logoLimits[0]) from your GPA example and it works! Thank you so much

– Sergey Khmelevskoy
Nov 15 '18 at 20:29







@TylerRoper Exactly what I was looking for, but unable to describe. I've added normalize(vScroll, topHeight, 0, logoLimits[1], logoLimits[0]) from your GPA example and it works! Thank you so much

– Sergey Khmelevskoy
Nov 15 '18 at 20:29














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326693%2fscale-logo-on-scroll-with-jquery-dependant-on-scroll-percentage%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326693%2fscale-logo-on-scroll-with-jquery-dependant-on-scroll-percentage%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine