使用本地存储更改网页背景

时间:2018-09-01 18:35:26

标签: javascript html css

很长一段时间以来,我一直想知道如何更改我编写代码的网页上的背景图片,但是当用户刷新我的网站时,我希望他们的更改仍然存在。就像他们单击更改背景一样,当他们刷新页面时,我希望背景仍然适合他们。我必须使用本地存储或cookie吗?在那种情况下,有人可以教我如何?

感谢您抽出宝贵的时间阅读本文,希望我得到一个好的答案。

我的代码示例:http://koda.nu/arkivet/33733671(密码:“ cwm”,全屏打开)。而且我确实尝试学习localStorage。

1 个答案:

答案 0 :(得分:3)

import numpy as np
from scipy import odr
import math

def CDF(x,a,b,c):
   #definition of errorfunction to fit data
   return c/2*(1+math.erf((x-a)/(b*math.sqrt(2))))

#to make things a little shorter I excluded the parts in which the data is 
read and prepared

guess=Ergebnis_popt[1] #using found values for a,b,c from curve_fit without 
errors
guess_a=guess[0]
guess_b=guess[1]
guess_c=guess[2]
xerr=Verschiebung_fit[2] #determination of x_erros, the values for the y 
errors are included in the prepared data
xerr=xerr/xerr
xerr=xerr/200
#building the model for the odr fit, as the erf function seems to only 
handle single values I use np.vectorize
cfd = odr.Model(np.vectorize(CDF,excluded=['a','b','c']),extra_args= 
['a','b','c'])  
#Definition of the Data with ererrors for the fit
mydata =odr.RealData(Verschiebung_fit[2],y=y_values_fit[2],sx=xerr,sy=y_values_fit[4])
#basis for the odr fit
odr=odr.ODR(mydata,cfd, beta0 = [guess_a,guess_b,guess_c] )
myoutput = odr.run()
myoutput.pprint()
var body = document.body;

// Apply if present in storage
if (localStorage.bgcolor) body.style.backgroundColor = localStorage.bgcolor;

// Set on button click
document.querySelectorAll("[data-bgcolor]").forEach(el =>
  el.addEventListener("click", function() {
body.style.backgroundColor = localStorage.bgcolor = this.getAttribute("data-bgcolor");
  })
);