简体   繁体   English

vuejs v-bind:style margin-right 工作但不是 margin-left

[英]vuejs v-bind:style margin-right working but not margin-left

So I'm having trouble with a :style on one of my div elements, when I do the following :因此,当我执行以下操作时,我的一个 div 元素上的 :style 出现问题:

:style="(index + 1) % 2 == (0) && type.Liste.length === indexVin + 1 ? `margin-left : calc((100% - (100% / ${type.Liste.length})) + 6rem);` : null"

this happens, as you can see the style attribute is empty发生这种情况,您可以看到 style 属性为空

div上没有样式

But if I change my margin-left to a margin-right or a padding-left it works just fine但是,如果我将我的margin-left更改为margin-rightpadding-left它工作得很好

具有右边距的 Div

左填充的 div

I read here that the margin-left should be in camelCase but I tried and nothing changed, weird thing is margin-right or padding-left are in kebab-case and work just fine.在这里读到margin-left应该在camelCase 中,但我尝试并没有改变,奇怪的是margin-rightpadding-left在kebab-case 中并且工作得很好。

I don't think it comes from my shorthand if since, once again it works whenever I set my css to anything but margin-left , I also tried removing any other source of css on my div (so only the one I setup here is applied) and that didn't work either.我不认为它来自我的速记,如果因为,每当我将我的 css 设置为除margin-left之外的任何东西时,它都会再次起作用,我还尝试删除我的 div 上的任何其他 css 源(所以只有我在这里设置的一个是应用),这也不起作用。

I'm running out of solutions, and yes I absolutely need it to be a margin instead of a padding我的解决方案已经用完了,是的,我绝对需要它作为边距而不是填充

Edit : Here's a bit more code, the weirdest thing happened, my console.log work just fine, triggering when it needs to but for some reason if I put margin-left in my if(even) it doesn't work however margin-left works when it enters the else if(odd) and it's the same for margin-right but in the opposite direction, margin-right only works when it's in the if(even) any idea why ?编辑:这里有更多代码,发生了最奇怪的事情,我的 console.log 工作得很好,在需要时触发但由于某种原因,如果我将 margin-left 放在我的if(even)中它不起作用但是 margin- left 在进入else if(odd)时有效,margin-right 相同,但方向相反,margin-right 仅在if(even)中有效,知道为什么吗?

 methods : { computedStyle(index , type , indexVin ) { if ((index + 1) % 2 == (0) && type.Liste.length === indexVin + 1) { console.log('even and last') return `margin-left : calc((100% - (100% / ${type.Liste.length})) + 6rem);` } else if (type.Liste.length === indexVin + 1) { console.log('odd and last') return `margin-right : calc((100% - (100% / ${type.Liste.length})) + 6rem);` // return `'margin-left' : calc((100% - (100% / ${type.Liste.length})) - 6rem);` // return 'background-color : red' } else { console.log('nothing special') return null } }, }
 <div class="padding-block-1" v-for="(type , index) in TypesDeVins" :key="index"> <div class="tw-flex tw-items-end slider-top" :class="[LeftOrRight(index) , FlexDirection(index)]"> <div class="colonnes-resp-2 tw-flex-shrink-0" :class="LeftOrRightMargin(index , 2)"> <h4 class="tw-uppercase tw-font-bold text-1-2 letter-spacing-3" :class="ColorTitle(index)">Les appellations</h4> <h2 class="tw-uppercase tw-text-white tw-font-bold text-2-4 tw-mb-12 letter-spacing-4">{{ type.Nom }}</h2> </div> <div class="swiper-container" :class="`slider-top-${index}`" :dir="rtl(index)"> <div class="swiper-wrapper"> <div class="nom-vin swiper-slide" v-for="(vin , indexVin) in type.Liste" :key="indexVin" :class="slideDir(index)" :style="computedStyle(index , type , indexVin)" > <h3 class="tw-text-white tw-font-bold tw-uppercase letter-spacing-3 text-1-2 tw-pb-12">{{ vin.title.rendered }}</h3> </div> </div> </div> </div> </div>

The documentation starts out by showing a style binding using an object: https://v2.vuejs.org/v2/guide/class-and-style.html该文档首先显示使用对象的样式绑定: https ://v2.vuejs.org/v2/guide/class-and-style.html

Here's an example from the docs:这是文档中的一个示例:

<div class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }"

Try this, also perhaps break out that style as a computed property to help readability and debugging.试试这个,也许也可以将这种风格作为一个计算属性来帮助可读性和调试。

template:模板:

<div :style="computedStyle(index)"></div>

script: (this needs to be added within computed not methods.)脚本:(这需要在计算的非方法中添加。)

computedStyle() {
  const listeLength = this.TypesDeVins[index].Liste.length;
  return index => {
    return (index + 1) % 2 == (0) && listeLength === indexVin + 1
        ? {'margin-left' : `calc((100% - (100% / ${listeLength})) + 6rem)`} 
        : null;
  }}

A couple notes:几点注意事项:

  • this computedStyle should be located within computed, not methods.这个 computedStyle 应该位于计算中,而不是方法中。
  • the error you received was correct, in my example type was not defined您收到的错误是正确的,在我的示例中类型未定义
  • type is a single item from your TypesDeVins array type 是 TypesDeVins 数组中的单个项目
  • i created a const to simply define the value directly you were keying off of我创建了一个 const 来简单地直接定义您要关闭的值

i recommend throwing in a debugger if needed to step through your code here to ensure the values are coming through as you expect them to.如果需要在此处单步执行您的代码,我建议您使用调试器,以确保值按照您的预期通过。

Based on your new code:根据您的新代码:

computed : {
  computedStyle() {
    return (index, indexVin) => {
      const listeLength = this.TypesDeVins[index].Liste.length;
      const spacing = `calc((100% - (100% / ${listeLength})) + 6rem)`;
      if ((index + 1) % 2 == (0) && listeLength === indexVin + 1) return {'margin-left' : spacing};
      if (listeLength === indexVin + 1) return {'margin-right' : spacing};

      return {};
    }
  }
},

Pay close attention to how parameters are used within a computed密切关注参数在计算中的使用方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM