What's the industry standard way to pass SASS variable to javascript?

后端 未结 4 767
夕颜
夕颜 2020-12-15 21:48

I have looked around and I\'ve seen one solution where in your html, you\'d have a tag dedicated to pass sass variables to javascript. I\'m talking about the second answer f

4条回答
  •  鱼传尺愫
    2020-12-15 22:19

    Not sure about "industry standard", but it's a very handy technique and not too difficult. The content of pseudo elements is not available via text() though, you have to use getComputedStyle.

    Example using body:after:

    Sass (using the compass breakpoint extension):

    body:after {
      display: none;
    
      @include breakpoint($bp-wide) {
        content: "wide";
      }
    
      @include breakpoint($bp-medium) {
        content: "medium";
      }
    
      @include breakpoint($bp-small) {
        content: "small";
      }
    }
    

    JavaScript:

    if (window.getComputedStyle) {
      var mq = window.getComputedStyle(document.body,':after').getPropertyValue('content');
    }
    
    if (mq.indexOf('small') !== -1) {
      // do something
    }
    

    Credit: I first saw this technique here: https://coderwall.com/p/_ldtkg

提交回复
热议问题