亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

切換一次更改 2 個 DIV 寬度 + localStorage

切換一次更改 2 個 DIV 寬度 + localStorage

尚方寶劍之說 2022-11-11 13:06:01
function myFunction() {   var element = document.getElementById("one1");   element.classList.toggle("one2");   var element = document.getElementById("two1");   element.classList.toggle("two2");}<style>#section{margin-left:50px;margin-right:50px}#monbouton{float:right;font-size:25px;width:50px;height:50px;background-color:#F1F1F1;border:1px solid ##F1F1F1}#one1{float:left;width:40%;height:100px;border:1px solid blue}.one2{width:10% !important;height:200px;border:1px solid red !important;}.one2 #afairedisparaitre{display:none}#two1{float:right;width:59%;height:100px;border:1px solid green}.two2{width:89% !important}</style><!DOCTYPE html><html><body><div id="section"><div id="one1"><button id="monbouton" onclick="myFunction()">&#8596;</button>    <div id="afairedisparaitre">This is DIV #one1<br />    Button toggle to CLASS .one2<br />  and reverse</div></div>  <div id="two1">This is DIV #two1<br />  Button toggle to CLASS .two2<br />  and reverse</div></div></body></html>我正在嘗試縮小我網站的左側,以便用戶覺得更舒適時可以擁有更寬的右側。我缺少的是一種在加載其他頁面時可以在整個站點上保留選擇的方法,直到用戶再次單擊為止。也許解決方案是在 "localStorage" 的 js 中多寫幾行?我真的很感激任何幫助。
查看完整描述

2 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

讓你的 CSS 更好一點?,F在我們只需要.with_toggle為#section.


它可以在這里,在 Snippet 中播下錯誤,但會在Codepan上很好地分叉,請參閱。嘗試切換它并在 Codepan 上重新加載頁面。


// checking if our storage is not empty

if (localStorage.toggled != '') {

  // set class to #section form storage value

  document.getElementById("section").classList.toggle(localStorage.toggled);

}


function myFunction() {

  if (localStorage.toggled != "with_toggle") {

    document.getElementById("section").classList.add("with_toggle");

    localStorage.toggled = "with_toggle";

  } else {

    document.getElementById("section").classList.remove("with_toggle");

    localStorage.toggled = "";

  }

}

#section {

  margin-left: 50px;

  margin-right: 50px;

}


#monbouton {

  float: right;

  font-size: 25px;

  width: 50px;

  height: 50px;

  background-color: #F1F1F1;

  border: 1px solid #F1F1F1;

}


#one1 {

  float: left;

  width: 40%;

  height: 100px;

  border: 1px solid blue;

}


.with_toggle #one1 {

  width: 10%;

  border: 1px solid red;

}


.with_toggle #one1 #afairedisparaitre {

  display: none;

}


#two1 {

  float: right;

  width: 59%;

  height: 100px;

  border: 1px solid green;

}


.with_toggle #two1 {

  width: 89%;

}

<div id="section">

  <div id="one1">

    <button id="monbouton" onclick="myFunction()">&#8596;</button>

    <div id="afairedisparaitre">This is DIV #one1<br /> Button toggle to CLASS .one2<br /> and reverse</div>

  </div>

  <div id="two1">This is DIV #two1<br /> Button toggle to CLASS .two2<br /> and reverse</div>

</div>


查看完整回答
反對 回復 2022-11-11
?
MM們

TA貢獻1886條經驗 獲得超2個贊

當然。只需創建一個 localStorage 變量來跟蹤收縮是否應該處于活動狀態,并使用它在頁面加載或類似的東西上應用您的樣式。


function shrinkActive() {

  var shrink;

  if (!(shrink = localStorage.getItem("shrink"))) {

    localStorage.setItem("shrink", "false");

    return false;

  }


  return JSON.parse(shrink);

}


function setShrink(active) {

  var element1 = document.getElementById("one1");

  var element2 = document.getElementById("two1");

  

  if (active) {

    element1.classList.add("one2");

    element2.classList.add("two2");

  } else {

    element1.classList.remove("one2");

    element2.classList.remove("two2");

  }

  

  localStorage.setItem("shrink", active.toString());

}


function myFunction() {

  setShrink(!shrinkActive());

}


window.onload = function() {

  setShrink(shrinkActive());

}

鏈接到工作的 Codepen。https://codepen.io/bugcatcher9000/pen/pogZbrz?editors=1111


查看完整回答
反對 回復 2022-11-11
  • 2 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號