`
ssrc0604hx
  • 浏览: 8331 次
文章分类
社区版块
存档分类
最新评论

说说开源中国社区的愚人节

 
阅读更多

今天是愚人节,上开源中国的首页http://www.oschina.net/,发现页面转到了起来:


哈哈,的确是被雷了一下。

不过这个实现需要css3的支持,所以ie9以下的ie是看不到的。


我们来看看这个功能的js:

/**
 * A little trick in April Fools Day
 * 2013-03-30 21:46 by Bluishoul
 */
$(function(){
	var date = new Date();
	var fools_image = "data:image/jpeg;base64,/9j/4AAQSkZJ.......RRF//Z";
	//if(date.getMonth()+1 === 4 && date.getDate() === 1){
		$("#41trick").remove();
		var count = 2;
		var tc = +$.cookie("trick");
		if(tc==2){
			$("body").css({
				"-webkit-transform":"none",
				"transform":"none"
			});
			$("#trick_words").remove();
		}else{
			$.cookie("trick",++tc,{expires: 7,path:''});
			$("body").css({
				"-webkit-transform":"rotate(-8deg)",
				"transform":"rotate(-8deg)"
			});
			if(tc==2){
				var width = ($(window).width() + 1000)/2 + 20;
				$("#OSC_Content table").append('<div id="trick_words" style="position:fixed;left:'+width+'px;top:125px;-webkit-transform:rotate(8deg);transform:rotate(8deg)"></div>');
				var img = new Image();
				img.src = fools_image;
				var p = $('<p style="text-align: center;font-size: 30px;margin-top: 21px;color: #A00;">愚人节快乐!</p>');
				$("#trick_words").append(img).append(p);
			}
		}
	//}
});

图片是使用编码后的数据,这里省略了,不然太长(推荐一个关于Data URI 博文:http://flysnowxf.iteye.com/blog/1271810)。


从上面的代码中,我们可以看到,先获取cookie中的 trick 值,这个值==2时,正常显示,不然就旋转 body 对象,这时设置了trick 这个cookie值为1。用户一看页面旋转了,一般会F5一下,还是旋转,这里cookie被设置为2,然后显示愚人节快乐的图片。用户再刷新,看不到旋转效果,因为cookie已经为2了。


这里,用到了$.cookie() 方法,这个是oschina自己扩展的方法,在oschina.js 189行:

//jQuery的cookie扩展
$.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


不过我觉得只是旋转一个小角度不够好,直接 style="transform: rotate(-180deg);":


这个效果太明显哈哈。


写这个文章,只在学习交流,不喜勿喷=.= 愚人节快乐



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics