Created
March 30, 2022 08:50
-
-
Save yeyuguo/bd51547b6b3c7ad18cfe3039d3d47c4a to your computer and use it in GitHub Desktop.
计算当前轮播可设置个数和宽度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getUsefulWidthCount({ | |
containerWidth, | |
cardWidth=356, | |
borderWidth=10, | |
}={}) { | |
if(!containerWidth || containerWidth<=0) return [0, 0] | |
let maxCountPer = 0, resultWith = 0 | |
const addNewCardWidth = () => (maxCountPer + 1) * (cardWidth + borderWidth) - borderWidth | |
while(cardWidth>0 && containerWidth>0 && addNewCardWidth() <= containerWidth) { | |
resultWith = addNewCardWidth() // 减去最后一个 | |
maxCountPer++ | |
} | |
const count = Math.max(0, maxCountPer) | |
return [count, resultWith] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment