Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yeyuguo/bd51547b6b3c7ad18cfe3039d3d47c4a to your computer and use it in GitHub Desktop.
Save yeyuguo/bd51547b6b3c7ad18cfe3039d3d47c4a to your computer and use it in GitHub Desktop.
计算当前轮播可设置个数和宽度
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