3

Html:

<ul class="nav nav-tabs nav-style">
  <tabs
    v-for="tabelement in tabelements" :name="tabelement":tabselected="tabelement == type ?  'active': ''" v-on:click="tabclick(tab)"
  ></tabs>
</ul>

JS:

Vue.component('tabs', {
  template:'<li :class="tabselected"><a href="#">{{name}}</a></li>',
  props:['name','tabselected']
});

I want to find the sum of width of all li in this example.

7
  • is it horizontal tab panel? Commented May 4, 2017 at 7:24
  • yes it is horizontal tab Commented May 4, 2017 at 8:03
  • so u need ul's width, dont you? jsFiddle Commented May 4, 2017 at 8:09
  • i think this will help. thanks Commented May 4, 2017 at 8:15
  • Is there any way i can loop through each li. get it individual width Commented May 4, 2017 at 8:16

1 Answer 1

5

Add watch block to your script.

script

watch: {
 'tabelements': function(val) {
   var lis = this.$refs.ul.getElementsByTagName("li");
   for (var i = 0, len = lis.length; i < len; i++) {
     console.log(lis[i].clientWidth); // do something
   }
   console.log(this.$refs.ul.clientWidth, this.$refs.ul.scrollWidth);
 }
}

if scrollWidth > clientWidth, u can show your arrows.

Updated. Explain Fiddle

template

<tabs ref="ul">

Put ref on component otherwise instance doesnot know about it

script

this.$nextTick This function run method when dom is updated

Sign up to request clarification or add additional context in comments.

1 Comment

it is not working for me . can you show it in this jsfiddle.net/k0ht01tj

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.