此时我有一个背景图片滑块,用户可以点击按钮并选择七个图像中的一个作为背景,当他们查看网站时,问题当然是当他们点击扔到一个不同的页面,它将恢复为默认图像。有没有办法让jquery.cookie.js来解决这个问题?以下是我的尝试。
有效的jQuery部分:
$(function(){
$('.btn a').parents('.btn').click(function(){
var img = $(this).attr('data-bg-img'),
afterFadeIn = function() {
$('body').css('background-image', 'url(' + img + ')');
$('bg-style').css('opacity', 0);
};
$('#bg-style')
.css({'background-image': 'url(' + img + ')', 'opacity': 0})
.animate({ 'opacity' : 1 }, 500, afterFadeIn);
});
});
没有的jQuery部分: $(function(){
if($.cookie("html_img")) {
$('html').css("background-image", $.cookie("html_img"));
}
$('.btn a').click(function() {
var image = 'imgs/someimage.jpg';
// var image = $(this).attr('src');
$('html').css("background-image", image);
$.cookie("html_img", image, {expires:7});
return false;
});
});
HTML:
<div id="slideshow" class="bgSlider">
<div class="btn btn1" data-bg-img ="<?php bloginfo('stylesheet_directory'); ?>/images/k2-in-skies.jpg"><a href="#">Button 1</a></div>
<div class="btn btn2" data-bg-img ="<?php bloginfo('stylesheet_directory'); ?>/images/k2.jpg" class="bgimage"><a href="#">Button 2</a></div>
<div class="btn btn3" data-bg-img ="<?php bloginfo('stylesheet_directory'); ?>/images/kunhar-river.jpg"><a href="#">Button 3</a></div>
<div class="btn btn4" data-bg-img ="<?php bloginfo('stylesheet_directory'); ?>/images/mitre-peak-baltoro.jpg"><a href="#">Button 4</a></div>
<div class="btn btn5" data-bg-img ="<?php bloginfo('stylesheet_directory'); ?>/images/musa-ka-musalla.jpg"><a href="#">Button 5</a></div>
<div class="btn btn6" data-bg-img ="<?php bloginfo('stylesheet_directory'); ?>/images/nanga-parbat.jpg"><a href="#">Button 6</a></div>
<div class="btn btn7" data-bg-img ="<?php bloginfo('stylesheet_directory'); ?>/images/naran-valley.jpg"><a href="#">Button 7</a></div>
<div class="clearfix"></div>
</div>
CSS:
#bg-style { width:100%; height: 700px; position:absolute; }
.btncontain { width:80%; margin: 0 auto; position:absolute; }
.btn a { width: 150px; height: 70px; background: yellow; float: left; margin: 0 30px 100px 30px; }
#bg-style {
background: url(../images/k2-in-skies.jpg) no-repeat center top;
}
非常感谢!
答案 0 :(得分:0)
这很方便我在cookie代码的第一行放了一个断点,然后把它拿回来了?
function (key, value, options) {
// key and at least value given, set cookie...
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
options = $.extend({}, options);
if (value === null || value === undefined) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
value = String(value);
return (document.cookie = [
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// key and possibly options given, get cookie...
options = value || {};
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
var pairs = document.cookie.split('; ');
for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
}
return null;
}