相信很多看博客园的朋友都想要一个可爱的珂朵莉在页面上陪着我们,那怎么实现呢?这里给大家带来教学。
需要准备:
javascript
html
css
keduoli.jpg
代码实现:
1.需要去百度上搜索jquery.min.js下载,版本应该问题不大
2.需要spig.js,github上也有的下载,这里我分享下我的
//右键菜单
jQuery(document).ready(function ($) {
$("#spig").mousedown(function (e) {
if(e.which==3){
showMessage("秘密通道:<br /> <img src=\"http://pic.sc.chinaz.com/Files/pic/Listfaces/3646/02.gif\"/><br/><a href=\"http://cnblogs.com/flipped\" title=\"水郁\">水郁</a> ",10000);
}
});
$("#spig").bind("contextmenu", function(e) {
return false;
});
});
//鼠标在消息上时
jQuery(document).ready(function ($) {
$("#message").hover(function () {
$("#message").fadeTo("100", 1);
});
});
//鼠标在上方时
jQuery(document).ready(function ($) {
$(".mumu").mouseover(function () {
$(".mumu").fadeTo("300", 0.3);
msgs = ["啊,主人不要嘛", "抓到我就让你嘿嘿嘿哦", "如果是主人的话,不用偷偷···", "珂朵莉想一直陪在主人身边"];
var i = Math.floor(Math.random() * msgs.length);
showMessage(msgs[i]);
});
$(".mumu").mouseout(function () {
$(".mumu").fadeTo("300", 1)
});
});
//开始
jQuery(document).ready(function ($) {
var url = window.location.href;
var title = document.title;
if (url.indexOf('/p/') < 0) { //如果是主页
var now = (new Date()).getHours();
if (now > 0 && now <= 6) {
showMessage(visitor + ' 夜深了,主人也早点休息吧', 6000);
} else if (now > 6 && now <= 11) {
showMessage(visitor + ' 主人今天还是这么早呢!早饭已经准备好了', 6000);
} else if (now > 11 && now <= 14) {
showMessage(visitor + '午餐时间到了,主人来享用午餐吧', 6000);
} else if (now > 14 && now <= 18) {
showMessage(visitor + ' 中午的时光真难熬!还好有你在!', 6000);
} else {
showMessage(visitor + ' 主人,能陪陪珂朵莉吗!', 6000);
}
}
else {
showMessage('欢迎' + visitor + '来到《' + title + '》', 6000);
}
$(".spig").animate({
top: $(".spig").offset().top + 300,
left: document.body.offsetWidth - 185
},
{
queue: false,
duration: 1000
});
// window.setTimeout(function () {
// showMessage("下面播报明日天气<iframe name=\"xidie\" src=\"http://api.lwl12.com/hitokoto/?encode=js\"frameborder=\“0\” scrolling=\"no\" width=\"130px\" allowtransparency=\"true\" ></iframe>", 10000);
// },
// 4000);
});
//鼠标在某些元素上方时
jQuery(document).ready(function ($) {
$('h2 a').click(function () {//标题被点击时
showMessage('萌萌地加载《<span style="color:#0099cc;">' + $(this).text() + '</span>》中,请稍候');
});
$('h2 a').mouseover(function () {
showMessage('要看看《<span style="color:#0099cc;">' + $(this).text() + '</span>》这篇随笔么?');
});
$('li a').mouseover(function () {
showMessage('去 <span style="color:#0099cc;">' + $(this).text() + '</span> 逛逛');
});
$('.tbCommentBody').mouseover(function () {
showMessage('<span style="color:#0099cc;">' + visitor + '</span> 向评论栏出发吧!');
});
$('#btn_comment_submit').mouseover(function () {
showMessage('确认提交了么?');
});
$('#q').focus(function () {
showMessage('输入你想搜索的关键词再按Enter(回车)键就可以搜索啦!');
});
});
//无聊讲点什么
jQuery(document).ready(function ($) {
window.setInterval(function () {
// msgs = [ "陪我聊天吧!", "好无聊哦,你都不陪我玩!", "…@……!………", "^%#&*!@*(&#)(!)(", "我可爱吧!嘻嘻!~^_^!~~","谁淫荡呀?~谁淫荡?,你淫荡呀!~~你淫荡!~~","从前有座山,山上有座庙,庙里有个老和尚给小和尚讲故事,讲:“从前有座……”"];
// msgs = ["<iframe src=\"http://api.lwl12.com/hitokoto\"frameborder=\"0\" scrolling=\"no\" id=\"test\" onload=\"this.height=50\" width=\"130px\" allowtransparency=\"true\" ></iframe>"];
//if(weather.state)msgs.push(weather.c[0],weather.c[2]);
//msgs = [weather.c[0],weather.c[2],"<iframe src=\"http://api.myhloli.com/hitokoto/\" frameborder=\"0\" scrolling=\"no\" id=\"external-frame\" height=\"70px\" width=\"150px\" allowtransparency=\"true\" ></iframe>","<iframe src=\"http://api.myhloli.com/hitokoto/\" frameborder=\"0\" scrolling=\"no\" id=\"external-frame\" height=\"70px\" width=\"150px\" allowtransparency=\"true\" ></iframe>","<iframe src=\"http://api.myhloli.com/hitokoto/\" frameborder=\"0\" scrolling=\"no\" id=\"external-frame\" height=\"70px\" width=\"150px\" allowtransparency=\"true\" ></iframe>"];
msgs = [$("#hitokoto").text()];
var i = Math.floor(Math.random() * msgs.length);
showMessage(msgs[i], 8000);
}, 15000);
});
/*
//无聊动动
jQuery(document).ready(function ($) {
window.setInterval(function () {
// msgs = ["播报明日天气<iframe name=\"xidie\" src=\"http://m.weather.com.cn/data/101010100.html\"frameborder=\“0\” scrolling=\"no\" height=\"15px\" width=\"130px\" allowtransparency=\"true\" ></iframe>", "乾坤大挪移!", "我飘过来了!~", "我飘过去了", "我得意地飘!~飘!~"];
// msgs = ["<iframe src=\"http://api.lwl12.com/hitokoto\"frameborder=\"0\" scrolling=\"no\" id=\"test\" onload=\"this.height=50\" width=\"140px\" allowtransparency=\"true\" ></iframe>"];
// if(weather.state)msgs.push(weather.c[0],weather.c[2]);
msgs = [weather.c[0],weather.c[2],"<iframe src=\"http://api.myhloli.com/hitokoto/\" frameborder=\"0\" scrolling=\"no\" id=\"external-frame\" height=\"70px\" width=\"150px\" allowtransparency=\"true\" ></iframe>","<iframe src=\"http://api.myhloli.com/hitokoto/\" frameborder=\"0\" scrolling=\"no\" id=\"external-frame\" height=\"70px\" width=\"150px\" allowtransparency=\"true\" ></iframe>","<iframe src=\"http://api.myhloli.com/hitokoto/\" frameborder=\"0\" scrolling=\"no\" id=\"external-frame\" height=\"70px\" width=\"150px\" allowtransparency=\"true\" ></iframe>"];
var i = Math.floor(Math.random() * msgs.length);
s = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6,0.7,0.75,-0.1, -0.2, -0.3, -0.4, -0.5, -0.6,-0.7,-0.75];
var i1 = Math.floor(Math.random() * s.length);
var i2 = Math.floor(Math.random() * s.length);
$(".spig").animate({
left: document.body.offsetWidth/2*(1+s[i1]),
top: document.body.offsetheight/2*(1+s[i1])
},
{
duration: 2000,
complete: showMessage(msgs[i])
});
}, 45000);
});
*/
//评论资料
jQuery(document).ready(function ($) {
$("#author").click(function () {
showMessage("留下你的尊姓大名!");
$(".spig").animate({
top: $("#author").offset().top - 70,
left: $("#author").offset().left - 170
},
{
queue: false,
duration: 1000
});
});
$("#email").click(function () {
showMessage("留下你的邮箱,不然就是无头像人士了!");
$(".spig").animate({
top: $("#email").offset().top - 70,
left: $("#email").offset().left - 170
},
{
queue: false,
duration: 1000
});
});
$("#url").click(function () {
showMessage("快快告诉我你的家在哪里,好让我去参观参观!");
$(".spig").animate({
top: $("#url").offset().top - 70,
left: $("#url").offset().left - 170
},
{
queue: false,
duration: 1000
});
});
$("#comment").click(function () {
showMessage("认真填写哦!不然会被认作垃圾评论的!我的乖乖~");
$(".spig").animate({
top: $("#comment").offset().top - 70,
left: $("#comment").offset().left - 170
},
{
queue: false,
duration: 1000
});
});
});
var spig_top = 50;
//滚动条移动
jQuery(document).ready(function ($) {
var f = $(".spig").offset().top;
$(window).scroll(function () {
$(".spig").animate({
top: $(window).scrollTop() + f +300
},
{
queue: false,
duration: 1000
});
});
});
//鼠标点击时
jQuery(document).ready(function ($) {
var stat_click = 0;
$(".mumu").click(function () {
if (!ismove) {
stat_click++;
if (stat_click > 4) {
msgs = ["再多宠爱珂朵莉一点", "主人,您已经摸我" + stat_click + "次了", "主人的抚摸,好舒服"];
var i = Math.floor(Math.random() * msgs.length);
//showMessage(msgs[i]);
} else {
msgs = ["珂朵莉还有点事,先走了", "嗯~呀啊!", "主人~不要···", "珂朵莉还是好害羞", "如果是主人的话,嗯~,没关系的"];
var i = Math.floor(Math.random() * msgs.length);
//showMessage(msgs[i]);
}
s = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6,0.7,0.75,-0.1, -0.2, -0.3, -0.4, -0.5, -0.6,-0.7,-0.75];
var i1 = Math.floor(Math.random() * s.length);
var i2 = Math.floor(Math.random() * s.length);
$(".spig").animate({
left: document.body.offsetWidth/2*(1+s[i1]),
top: document.body.offsetheight/2*(1+s[i1])
},
{
duration: 500,
complete: showMessage(msgs[i])
});
} else {
ismove = false;
}
});
});
//显示消息函数
function showMessage(a, b) {
if (b == null) b = 10000;
jQuery("#message").hide().stop();
jQuery("#message").html(a);
jQuery("#message").fadeIn();
jQuery("#message").fadeTo("1", 1);
jQuery("#message").fadeOut(b);
};
//拖动
var _move = false;
var ismove = false; //移动标记
var _x, _y; //鼠标离控件左上角的相对位置
jQuery(document).ready(function ($) {
$("#spig").mousedown(function (e) {
_move = true;
_x = e.pageX - parseInt($("#spig").css("left"));
_y = e.pageY - parseInt($("#spig").css("top"));
});
$(document).mousemove(function (e) {
if (_move) {
var x = e.pageX - _x;
var y = e.pageY - _y;
var wx = $(window).width() - $('#spig').width();
var dy = $(document).height() - $('#spig').height();
if(x >= 0 && x <= wx && y > 0 && y <= dy) {
$("#spig").css({
top: y,
left: x
}); //控件新位置
ismove = true;
}
}
}).mouseup(function () {
_move = false;
});
});
3.css实现动态效果
.spig {
display:block;
width:175px;
height:246px;
position:absolute;
bottom: 300px;
left:180px;
z-index:9999;
}
#message{
color :#191919;
border: 1px solid #c4c4c4;
background:#ddd;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
min-height:1em;
padding:5px;
top:-45px;
position:absolute;
text-align:center;
width:auto !important;
z-index:10000;
-moz-box-shadow:0 0 15px #eeeeee;
-webkit-box-shadow:0 0 15px #eeeeee;
border-color:#eeeeee;
box-shadow:0 0 15px #eeeeee;
outline:none;
}
.mumu{
width:100px;
height:200px;
cursor:move;
background:url(kdl.png) no-repeat;
}
4.html实现
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="spig.js"></script>
<link rel="stylesheet" href="digit.css" type="text/css"/>
<body style="height:1000px;">
<!--小人只在首页显示 start*-->
<div id="spig" class="spig">
<div id="message">哦海yo,狗秀金sama</div>
<div id="mumu" class="mumu"></div>
</div>
</body>