4

I have this code that uses data attributes to display ruby text but I would like the main text not to change position upon toggle. Ordinarily one would use visibility: hidden but that doesn't seem to apply in this scenario. What other options are there?

$(function() {
  $("#furigana").click(function() {
    $(".jp").toggleClass("furigana_enabled", this.checked);
  });
});
.jp {
  font-size: 20pt;
}

.furigana_enabled {
  line-height: 2.1em;
}

.furigana_enabled .word {
  margin-right: 0.5em;
  position: relative;
  top: 0.6em;
  display: inline-block;
  white-space: nowrap;
}

.furigana_enabled .kanji::before {
  content: attr(data-reading);
  color: gray;
  font-size: 50%;
  line-height: 1;
  position: absolute;
  top: -0.5em;
  white-space: nowrap;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div>
  <input type="checkbox" id="furigana" checked /><label for="furigana">
    Furigana</label
  >
</div>
<hr />
<div class="jp japanese_gothic furigana_enabled">
  <span class="word"><span class="kanji" data-reading="きのう">昨日</span></span>

  <span class="word" title="noun"
    >すき<span class="kanji" data-reading="や">焼</span>き</span>

  <span class="word" title="particle">を</span>

  <span class="word" title="verb"><span class="kanji" data-reading="た">食</span>べました</span>

  <span class="word" title="punctuation">。</span>
</div>

Here's a link to the code

Awais
  • 4,752
  • 4
  • 17
  • 40
quietplace
  • 471
  • 1
  • 4
  • 14

1 Answers1

4

By using visibility you can achieve this

I set text before adding class and set it to hidden then on click show its visibility.

$(function() {
  $("#furigana").click(function() {
    $(".jp").toggleClass("furigana_enabled", this.checked);
  });
});
.jp {
  font-size: 20pt;
  line-height: 2.1em;
}
.word {
  margin-right: 0.5em;
  position: relative;
  top: 0.6em;
  display: inline-block;
  white-space: nowrap;
}
.kanji::before {
  content: attr(data-reading);
  color: gray;
  font-size: 50%;
  line-height: 1;
  position: absolute;
  top: -0.5em;
  white-space: nowrap;
  visibility: hidden;
}
.furigana_enabled .kanji::before {
  content: attr(data-reading);
  color: gray;
  font-size: 50%;
  line-height: 1;
  position: absolute;
  top: -0.5em;
  white-space: nowrap;
  visibility: visible;
}
.japanese_gothic {
  font-family: "HiraKakuPro-W3", "Hiragino Kaku Gothic Pro W3", "Hiragino Kaku Gothic Pro", "ヒラギノ角ゴ Pro W3", "メイリオ", Meiryo, "游ゴシック", YuGothic, "MS Pゴシック", "MS PGothic", "MS ゴシック", "MS Gothic", sans-serif;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div><input type="checkbox" id="furigana" checked><label for="furigana"> Furigana</label></div>
<hr>
<div class="jp japanese_gothic furigana_enabled">
  
<span class="word"><span class="kanji" data-reading="きのう">昨日</span></span>
  
 <span class="word" title="noun">すき<span class="kanji" data-reading="や">焼</span>き</span>
  
<span class="word" title="particle">を</span>
  
<span class="word" title="verb"><span class="kanji" data-reading="た">食</span>べました</span>
 
<span class="word" title="punctuation">。</span>
  
</div>
Awais
  • 4,752
  • 4
  • 17
  • 40