Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--------- jsの読み込み --------->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>

<!--------- 文字サイズ変更ボタン --------->
<div class="btn_wrap">
    <span>文字の大きさ</span>
    <button type="button" class="sizeBtn" id="font-small">小</button>
    <button type="button" class="sizeBtn is_active" id="font-middle">中</button>
    <button type="button" class="sizeBtn" id="font-large">大</button>
</div>
<!--------- 背景色変更ボタン --------->
<div class="btn_wrap">
    <span>背景の色</span>
    <button type="button" class="bgBtn is_active" id="bg-white">白</button>
    <button type="button" class="bgBtn" id="bg-blue">青</button>
    <button type="button" class="bgBtn" id="bg-black">黒</button>
</div>


<div class="content">
  今回は「jquery-cookie」というライブラリを使って、文字サイズを変更できるボタン、背景色を変更できるボタンを実装していきます。今回は「jquery-cookie」というライブラリを使って、文字サイズを変更できるボタン、背景色を変更できるボタンを実装していきます。今回は「jquery-cookie」というライブラリを使って、文字サイズを変更できるボタン、背景色を変更できるボタンを実装していきます。今回は「jquery-cookie」というライブラリを使って、文字サイズを変更できるボタン、背景色を変更できるボタンを実装していきます。
</div>
              
            
!

CSS

              
                :root {
  --pink-to-yellow: #ff82a5;
  --to-bgColor: white;
  --to-white: black;
}
html {
  font-size: 62.5%;
  transition: .2s;
}
body {
  font-size: 1.6rem;
  transition: .2s;
}
button:hover {
  cursor: pointer;
}

@media (max-width: 768px) {
    body {
        font-size: 1.4rem;
    }
}

.content {
  padding: 20px
}
.btn_wrap {
  margin: 20px;
}
.btn_wrap button {
  font-size: 1.6rem;
  padding: 5px 10px;
  border: 1px solid #d9d9d9;
  background: transparent;
  color: var(--to-white);
}
.btn_wrap button.is_active {
  background: var(--pink-to-yellow);
  color: var(--to-bgColor);
}
              
            
!

JS

              
                $(function () {
  /* -------------------------
  // 背景色変更
  -------------------------- */
  let bg = $.cookie('bgColor');
  // cookieに値が保存されていた場合
  if (bg) {
    $('.bgBtn').removeClass('is_active');
    if (bg == 'bg-white') { 
      bgWhite();
    } else if (bg == 'bg-blue') { 
      bgBlue();
    } else if (bg == 'bg-black') { 
      bgBlack();
    }
  }

  $('.bgBtn').click(function () {
    $.cookie('bgColor', this.id, { expires: 7, path:'/' });
    $('.bgBtn').removeClass('is_active');
    if (this.id == 'bg-white') {
      bgWhite();
    } else if (this.id == 'bg-blue') { 
      bgBlue();
    } else if (this.id == 'bg-black') { 
      bgBlack();
    }
  });

  //背景色:白
  function bgWhite(){
    $('#bg-white').addClass('is_active');
    $('html').css('background', 'white'); //背景(白)
    $('html').css('color', 'black'); //文字色(黒)
    $(':root').css({
      "--to-white": 'black',
      "--pink-to-yellow": '#ff82a5',
      "--to-bgColor": 'white'
    });
  }
  //背景色:青
  function bgBlue(){
    $('#bg-blue').addClass('is_active');
    $('html').css('background', 'blue'); //背景(青)
    $('html').css('color', 'white'); //文字色(白)
    $(':root').css({
      "--to-white": 'white',
      "--pink-to-yellow": 'yellow',
      "--to-bgColor": 'blue'
    });
  }
  //背景色:黒
  function bgBlack(){
    $('#bg-black').addClass('is_active');
    $('html').css('background', 'black'); //背景(黒)
    $('html').css('color', 'white'); //文字色(白)
    $(':root').css({
      "--to-white": 'white',
      "--pink-to-yellow": 'yellow',
      "--to-bgColor": 'black'
    });
  }


  /* -------------------------
  // 文字サイズ変更
  -------------------------- */
  let size = $.cookie('fontSize');

  // cookieに値が保存されていた場合
  if (size) {
    $('.sizeBtn').removeClass('is_active');
    if (size == 'font-small') {
      fontSmall();
    } else if (size == 'font-middle') {
      fontMiddle();
    } else if (size == 'font-large') {
      fontLarge();
    }
  }
  
  $('.sizeBtn').click(function () {
    $.cookie('fontSize', this.id, { expires: 7, path:'/' });
    $('.sizeBtn').removeClass('is_active');
    if (this.id == 'font-small') {
      fontSmall();
    } else if (this.id == 'font-middle') {
      fontMiddle();
    } else if (this.id == 'font-large') {
      fontLarge();
    }
  });

  //文字サイズ:小
  function fontSmall(){
    $('#font-small').addClass('is_active');
    if(window.matchMedia("(max-width: 768px)").matches){
      $('body').css('font-size', '1.3rem');
    }else{ 
      $('body').css('font-size', '1.4rem');
    }
  }
  //文字サイズ:中
  function fontMiddle(){
    $('#font-middle').addClass('is_active');
    if(window.matchMedia("(max-width: 768px)").matches){
      $('body').css('font-size', '1.4rem');
    }else{ 
      $('body').css('font-size', '1.6rem');
    }
  }
  //文字サイズ:大
  function fontLarge(){
    $('#font-large').addClass('is_active');
    if(window.matchMedia("(max-width: 768px)").matches){
      $('body').css('font-size', '1.6rem');
    }else{ 
      $('body').css('font-size', '1.8rem');
    }
  }
});
              
            
!
999px

Console