gpt4 book ai didi

javascript - 为 flexGrow 编写数学公式以调整 div 宽度,将字母数和窗口宽度作为变量

转载 作者:行者123 更新时间:2023-12-04 10:47:40 26 4
gpt4 key购买 nike

如果你运行下面的代码片段,你会看到我在每个 div 上有一个悬停效果,这会打开一个立即购买按钮..为了使这个工作适用于不同的语言和不同的窗口宽度,我希望使每个 div 的宽度动态取决于“立即购买”div 和窗口宽度中有多少个字母。所以我做了一个时髦的公式,但不确定如何让它在不同的窗口宽度上工作。有人可以帮我做数学吗?

变量是:
flex 成长,
窗口宽度 &
buyNowLetters(div 中的字母数)

    document.addEventListener('DOMContentLoaded', fms())

function fms(){
var shadeFinder = document.querySelector('.fms-wrapper')
var shadeDiv = document.querySelectorAll('.fms-shade')
var vw = window.innerWidth;

//first load the right colors in to the divs
shadeDiv.forEach(div => {
div.style.backgroundColor = div.getAttribute("fms-bg")
})

//mouse hover events
shadeDiv.forEach(div => {
div.onmouseenter = function(event) {
let target = event.target;
target.classList.add('js-hover')
var buyNowLetters = target.lastChild.innerHTML.length;

//THIS IS THE FORMULA IVE BEEN TRYING WITH
target.style.flexGrow = (20000/vw)*(buyNowLetters/19);
};

div.onmouseleave = function(event) {
let target = event.target;
target.style.background = target.getAttribute("fms-bg");
target.classList.remove('js-hover')
target.style.flexGrow = 1;
};
})

}
.fms-wrapper {
height: 300px;
width: 100%;
display: flex;
margin: 100px 0;
align-items: center;
}

.fms-shade {

flex-basis: auto;
flex-grow: 1;
transition: all 0.8s;
height: 100%;
transform: scaleY(1);
position: relative;
margin-left:-1px;
overflow: hidden;
justify-content: center;
display: flex;
text-align: center;
transition-timing-function: ease-in;
}

.fms-shade.js-hover {

transition: all 0.2s;
height: 120%;
transition-timing-function: ease-out;
}


/* top bottom text before hover */
.fms-shade > .fms-shade__top {
position: absolute;
display: inline-block;
opacity: 0;
transition: all 0.2s;
}
.fms-shade > .fms-shade__bottom {
position: absolute;
display: inline-block;
opacity: 0;
transition: all 0.2s;
bottom: 10px;
white-space: nowrap;
}


/* hover over div */
.fms-shade.js-hover > .fms-shade__top {
position: absolute;
opacity: 1;
display: block;
text-align: center;
transition: all 0.2s;
}
.fms-shade.js-hover > .fms-shade__bottom {
position: absolute;
opacity: 1;
display: block;
text-align: center;
bottom: 10px;
transition: all 0.2s;
}
<div class="fms-wrapper">
<div f1 class="fms-shade" fms-bg="rgb(189, 72, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">B </p></div>
<div f2 class="fms-shade" fms-bg="rgb(65, 104, 148)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BW </p></div>
<div f3 class="fms-shade" fms-bg="rgb(95, 189, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BU W</p></div>
<div f4 class="fms-shade" fms-bg="rgb(189, 168, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BU NW</p></div>
<div f5 class="fms-shade" fms-bg="rgb(189, 72, 183)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BU YOW</p></div>
<div f6 class="fms-shade" fms-bg="rgb(189, 72, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUY NOW</p></div>
<div f7 class="fms-shade" fms-bg="rgb(65, 104, 148)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUY NOWW</p></div>
<div f8 class="fms-shade" fms-bg="rgb(95, 189, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUY NOWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUY NOWWW</p></div>
<div f11 class="fms-shade" fms-bg="rgb(189, 72, 183)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUU YNOWWWW</p></div>

<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWWWW</p></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWWWWWW</p></div>

<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>

<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>

<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>

<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
<div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
<div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>


</div>


我什至不确定 flexGrow 是否是最好的方法,也许有更好的方法?

最佳答案

当 div 为空时,您有一个 javascript 错误需要解决:

if(target.lastChild != null)

解决方案:

you might need to small changes on the code, but this to give an idea of how to calculate the width


  • 使用以下 jquery 获取折叠条的宽度

    if you have another idea you can implement I'm short in time thats why i used the jquery:

  • const barWidth =$('.fms-shade').sort(function(a, b){
    return $(a).width() > $(b).width();
    }).first().width();
  • 使用以下方法获取当前条形内容宽度:
  • var contentWidth =target.lastChild.clientWidth;
  • 使用以下代码计算您必须将折叠的 div 宽度乘以多少次才能显示内容。

    Math.ceil to get the nearest bigger integer

  • var flexMCount = Math.ceil(contentWidth/barWidth); 
  • 将数字分配给 flexgrow:
  • target.style.flexGrow =flexMCount;

    检查完整代码:

    document.addEventListener('DOMContentLoaded', fms())

    const barWidth =$('.fms-shade').sort(function(a, b){
    return $(a).width() > $(b).width();
    }).first().width();

    function fms(){
    var shadeFinder = document.querySelector('.fms-wrapper')
    var shadeDiv = document.querySelectorAll('.fms-shade')
    var vw = window.innerWidth;

    //first load the right colors in to the divs
    shadeDiv.forEach(div => {
    div.style.backgroundColor = div.getAttribute("fms-bg")
    })

    //mouse hover events
    shadeDiv.forEach(div => {
    div.onmouseenter = function(event) {
    let target = event.target;
    target.classList.add('js-hover')
    if(target.lastChild != null)
    {
    //Get the collapsed bar width
    var contentWidth =target.lastChild.clientWidth;
    var flexMCount = Math.ceil(contentWidth/barWidth);


    //THIS IS THE FORMULA IVE BEEN TRYING WITH
    target.style.flexGrow =flexMCount;
    }
    };

    div.onmouseleave = function(event) {
    let target = event.target;
    target.style.background = target.getAttribute("fms-bg");
    target.classList.remove('js-hover')
    target.style.flexGrow = 1;
    };
    })

    }
    .fms-wrapper {
    height: 300px;
    width: 100%;
    display: flex;
    margin: 100px 0;
    align-items: center;
    }

    .fms-shade {

    flex-basis: auto;
    flex-grow: 1;
    transition: all 0.8s;
    height: 100%;
    transform: scaleY(1);
    position: relative;
    margin-left:-1px;
    overflow: hidden;
    justify-content: center;
    display: flex;
    text-align: center;
    transition-timing-function: ease-in;
    }

    .fms-shade.js-hover {

    transition: all 0.2s;
    height: 120%;
    transition-timing-function: ease-out;
    }


    /* top bottom text before hover */
    .fms-shade > .fms-shade__top {
    position: absolute;
    display: inline-block;
    opacity: 0;
    transition: all 0.2s;
    }
    .fms-shade > .fms-shade__bottom {
    position: absolute;
    display: inline-block;
    opacity: 0;
    transition: all 0.2s;
    bottom: 10px;
    white-space: nowrap;
    }


    /* hover over div */
    .fms-shade.js-hover > .fms-shade__top {
    position: absolute;
    opacity: 1;
    display: block;
    text-align: center;
    transition: all 0.2s;
    }
    .fms-shade.js-hover > .fms-shade__bottom {
    position: absolute;
    opacity: 1;
    display: block;
    text-align: center;
    bottom: 10px;
    transition: all 0.2s;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="fms-wrapper">
    <div f1 class="fms-shade" fms-bg="rgb(189, 72, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">B </p></div>
    <div f2 class="fms-shade" fms-bg="rgb(65, 104, 148)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BW </p></div>
    <div f3 class="fms-shade" fms-bg="rgb(95, 189, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BU W</p></div>
    <div f4 class="fms-shade" fms-bg="rgb(189, 168, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BU NW</p></div>
    <div f5 class="fms-shade" fms-bg="rgb(189, 72, 183)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BU YOW</p></div>
    <div f6 class="fms-shade" fms-bg="rgb(189, 72, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUY NOW</p></div>
    <div f7 class="fms-shade" fms-bg="rgb(65, 104, 148)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUY NOWW</p></div>
    <div f8 class="fms-shade" fms-bg="rgb(95, 189, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUY NOWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUY NOWWW</p></div>
    <div f11 class="fms-shade" fms-bg="rgb(189, 72, 183)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUU YNOWWWW</p></div>

    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWWWW</p></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"><p class="fms-shade__top">F9</p><p class="fms-shade__bottom">BUUYNOWWWWWWWWWWWBUUYNOWWWWWWWWWWW</p></div>

    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>

    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>

    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>

    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(65, 104, 148)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(95, 189, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 168, 72)"></div>
    <div f9 class="fms-shade" fms-bg="rgb(189, 72, 183)"></div>


    </div>



    Upvote if you like it

    关于javascript - 为 flexGrow 编写数学公式以调整 div 宽度,将字母数和窗口宽度作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59626603/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com