// Defines a jquery plugin.
; (function($) {
$.fn.weiboSearch = function() {
// your plugin logic
};
})(jQuery);
// Defines weibo defaults type.
$.fn.weiboSearch.defaults = {
url: 'https://api.weibo.com/2/search/topics.json?q=',
appKey: '5786724301',
numWeibo: 15,
term: ''
};
$.getJSONP = function(s) {
// Due to cross origin request, so we to use jsonp format.
s.dataType = "jsonp";
$.ajax(s);
// figure out what the callback fn is
var $script = $(document.getElementsByTagName('head')[0].firstChild);
var url = $script.attr('src') || '';
// Gets callback function
var cb = (url.match(/callback=(\w+)/) || [])[1];
if (!cb)
return; // bail
var t = 0, cbFn = window[cb];
$script[0].onerror = function(e) {
$script.remove();
handleError(s, {}, "error", e);
clearTimeout(t);
};
if (!s.timeout)
return;
window[cb] = function(json) {
clearTimeout(t);
cbFn(json);
cbFn = null;
};
// Gets time out function flag.
t = setTimeout(function() {
$script.remove();
handleError(s, {}, "timeout");
if (cbFn)
window[cb] = function() {
};
}, s.timeout);
/**
* Fix issue: "jQuery.handleError is not a function"
*/
function handleError(s, xhr, msg, e) {
s.error && s.error.call(s.context, xhr, msg, e);
s.global && $.event.trigger("ajaxError", [xhr, s, e || msg]);
s.complete && s.complete.call(s.context, xhr, e || msg);
}
};
/**
* Uses ajax request to grab weibos.
*/
function grabWeibos() {
var url = opts.url;
grabFlag = false;
grabbing = true;
$.getJSONP({
url: url,
timeout: 30000,
data: {
source: opts.appKey,
q: opts.term,
count: opts.numWeibo
},
error: function(xhr, status, e) {
},
complete: function() {
},
success: function(json) {
if (json.error) {
// Can't get results displays error.
return;
}
// iterates weibo results
$.each(json.data.statuses, function(i) {
// Adds data to page.
})
}
});
}
<!-- From design-->
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title></title>
<link rel="stylesheet" type="text/css" href="css/weibo.serach.style.css">
</head>
<body>
<table>
<tbody>
<tr>
<td>
<div id="weibo1" class="weibo">
</div>
</td>
<td>
<div id="weibo2" class="weibo">
</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
<!-- Adds Javascript reference --> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.weibo.search.js"></script>
<!-- When document ready invokes charCount function-->
<script type="text/javascript">
// Invokes webioSearch function.
$(document).ready(function () {
$("#weibo1").weiboSearch({
term:'情人节',
direction:'down'
});
$("#weibo2").weiboSearch({
term:'元宵节',
direction:'up'
});
});
</script>
// Gets response data from weibo api.
success: function(json) {
if (json.data.error) {
// Can't get data displays error.
failEye(json.data.error);
return;
}
// Emptys contain with fade out effect.
$cont.fadeOut('fast', function() {
$cont.empty();
// iterates weibo results
$.each(json.data.statuses, function(i) {
if (!opts.filter.call(opts, this) || this.truncated)
return; // skip this weibo, some weibos may be deleted.
var $img, $text, w,
tweet = opts.formatter(this, opts),
$tweet = $(tweet);
// Weibo data.
$tweet.css(opts.css['tweet']);
$img = $tweet.find('.weiboSearchProfileImg').css(opts.css['img']);
$tweet.find('.weiboSearchUser').css(opts.css['user']);
$tweet.find('.weiboSearchTime').css(opts.css['time']);
$tweet.find('a').css(opts.css['a']);
$tweet.appendTo($cont);
$text = $tweet.find('.weiboSearchText').css(opts.css['text']);
if (opts.avatar) {
w = $img.outerWidth() + parseInt($tweet.css('paddingLeft'));
$text.css('paddingLeft', w);
}
})
// Loads weibos with fade in effect.
$cont.fadeIn('fast');
// Invokes weibo api again.
if (json.data.statuses.length < 2) {
if (opts.refreshSeconds)
setTimeout(gradWeibos, opts.refreshSeconds * 1000);
return;
}
});
}
function relativeTime(dateString) {
var values = dateString.split(" ");
dateString = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
var parsed_date = Date.parse(dateString);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
delta = delta + (relative_to.getTimezoneOffset() * 60);
if (delta < 60) {
return 'just now';
} else if (delta < 120) {
return 'a minute ago';
} else if (delta < (60 * 60)) {
return (parseInt(delta / 60)).toString() + ' minutes ago';
} else if (delta < (120 * 60)) {
return 'about an hour ago';
} else if (delta < (24 * 60 * 60)) {
return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
} else if (delta < (48 * 60 * 60)) {
return '1 day ago';
} else {
return (parseInt(delta / 86400)).toString() + ' days ago';
}
}
/**
* Weibos rolling from top to bottom
*/
function weiboIn() {
if (paused || grabbing) {
setTimeout(weiboIn, 500);
return;
}
// Gets last element.
var h, $el = $cont.children(':last'), $elFirst = $cont.children(':first');
// Gets last weibo item height.
h = $el.outerHeight();
// Animate: increases the first weibo item margin top to 'h'.
// Then decreases the first weibo item margin top to '0'.
$elFirst.animate({ marginTop: h }, opts.animInSpeed, function() {
$elFirst.css({ marginTop: 0, opacity: 1 });
/*@cc_on
try { el.style.removeAttribute('filter'); } // ie cleartype fix
catch (smother) { }
@*/
// append the last weibo item first.
$el.css(opts.css['tweet']).hide().prependTo($cont);
// Fade in display new item.
$el.fadeIn(opts.animInSpeed);
// Loop
setTimeout(grabFlag ? grabWeibos : weiboIn, opts.timeout);
});
}
/**
* Weibos rolling from bottom to top.
*/
function weiboOut() {
if (paused || grabbing) {
setTimeout(weiboOut, 500);
return;
}
// Gets last element.
var h, $el = $cont.children(':first'), el = $el[0];
// Implements fade out effect.
$el.animate(opts.animOut, opts.animOutSpeed, function() {
// Gets first weibo item height.
h = $el.outerHeight();
$el.animate({ marginTop: -h }, opts.animInSpeed, function() {
$el.css({ marginTop: 0, opacity: 1 });
/*@cc_on
try { el.style.removeAttribute('filter'); } // ie cleartype fix
catch (smother) { }
@*/
// append the last weibo item last.
$el.css(opts.css['tweet']).show().appendTo($cont);
setTimeout(grabFlag ? grabWeibos : weiboOut, opts.timeout);
});
});
}
// Weibo css style in jquery plugin.
css:{
// default styling
a:{ textDecoration:'none', color:'#3B5998' },
eye:{ width:'40px', height:'40px', position:'absolute', left:'-30px', top:'-20px', border:'none' },
container:{ overflow:'hidden', backgroundColor:'#eee', height:'100%' },
fail:{ background:'#6cc5c3 url(./images/error_page_small.png) no-repeat 50% 50%', height:'100%', padding:'10px' },
frame:{ border:'10px solid #C2CFF1', borderRadius:'10px', '-moz-border-radius':'10px', '-webkit-border-radius':'10px' },
tweet:{ padding:'5px 10px', clear:'left' },
img:{ 'float':'left', margin:'5px', width:'48px', height:'48px' },
loading:{ padding:'20px', textAlign:'center', color:'#888' },
text:{},
time:{ fontSize:'smaller', color:'#888' },
title:{ backgroundColor:'#C2CFF1', margin:0, padding:'0 0 5px 0', textAlign:'center', fontWeight:'bold', fontSize:'large', position:'relative' },
titleLink:{ textDecoration:'none', color:'#3B5998' },
user:{ fontWeight:'bold' }
}
然后,我们weibo.serach.style.css文件中添加以下样式,具体定义如下:
div.weibo { margin: auto; width: 300px }
#weibo1 { height: 300px;}
#weibo2 { height: 300px; }
body { background-color: white }
body, div { font-family: '微软雅黑', helvetica, verdana, arial, sans-serif }
body { margin: 20px 0; padding: 0; font-size: small; color: #333 }
div {display: block}
/* Image rounded corner*/
.weiboSearchProfileImg{
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
}
table {
margin: auto;
border-collapse: separate;
border-spacing: 25px;
}
table {
border-collapse: collapse;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有