|
/* |
|
Code by Gabriel Nunes |
|
用到了jQuery UI框架,这里要了解。 |
|
*/ |
|
|
|
function inIframe () { try { return window.self !== window.top; } catch (e) { return true; } } |
|
/* |
|
window.self |
|
|
|
|
|
|
|
功能:是对当前窗口自身的引用。它和window属性是等价的。 |
|
|
|
语法:window.self |
|
|
|
注:window、self、window.self是等价的。 |
|
|
|
|
|
|
|
window.top |
|
|
|
功能:返回顶层窗口,即浏览器窗口。 |
|
|
|
语法:window.top |
|
|
|
注:如果窗口本身就是顶层窗口,top属性返回的是对自身的引用。 |
|
window.parent |
|
|
|
功能:返回父窗口。 |
|
|
|
语法:window.parent |
|
|
|
注:如果窗口本身是顶层窗口,parent属性返回的是对自身的引用。 |
|
|
|
在框架网页中,一般父窗口就是顶层窗口,但如果框架中还有框架,父窗口和顶层窗口就不一定相同了。 |
|
判断当前窗口是否在一个框架中: |
|
|
|
<script type="text/javascript"> |
|
var b = window.top!=window.self; |
|
document.write( "当前窗口是否在一个框架中:"+b ); |
|
</script> |
|
上面的程序显然是判断当前窗口是否在该框架中了。 |
|
|
|
使用框架的好处是可以在一个浏览器窗口中显示多个webpage。 |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
var colors = ['#16a085', '#27ae60', '#2c3e50', '#f39c12', '#e74c3c', '#9b59b6', '#FB6964', '#342224', "#472E32", "#BDBB99", "#77B1A9", "#73A857"]; |
|
//定义定义了12种颜色 |
|
var currentQuote = '', currentAuthor = ''; |
|
//定义了当前引语和当期当前当前作者两个字符串 |
|
function openURL(url){ |
|
window.open(url, 'Share', 'width=550, height=400, toolbar=0, scrollbars=1 ,location=0 ,statusbar=0,menubar=0, resizable=0'); |
|
} |
|
//定义了一个函数,用于打开URL |
|
function getQuote() { |
|
$.ajax({ |
|
headers: { |
|
"X-Mashape-Key": "OivH71yd3tmshl9YKzFH7BTzBVRQp1RaKLajsnafgL2aPsfP9V", |
|
Accept: "application/json", |
|
"Content-Type": "application/x-www-form-urlencoded" |
|
}, |
|
url: 'https://andruxnet-random-famous-quotes.p.mashape.com/cat=', |
|
success: function(response) { |
|
var r = JSON.parse(response); |
|
currentQuote = r.quote; |
|
currentAuthor = r.author; |
|
if(inIframe()) |
|
{ |
|
$('#tweet-quote').attr('href', 'https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=' + encodeURIComponent('"' + currentQuote + '" ' + currentAuthor)); |
|
/* |
|
jQuery中的attr()方法可以用来改变属性值,这里的attr也就是attribute了,在这里href算是tweet-quote这个id的一个属性,因此可以进行改变。我在之前的程序中,就是这里出了问题,不会让这个内容动态变化。另外需要注意encodeURIComponent这个方法的使用,他能够将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同) |
|
|
|
|
|
*/ |
|
|
|
|
|
$('#tumblr-quote').attr('href', 'https://www.tumblr.com/widgets/share/tool?posttype=quote&tags=quotes,freecodecamp&caption='+encodeURIComponent(currentAuthor)+'&content=' + encodeURIComponent(currentQuote)); |
|
} |
|
//下面的animate()方法的主要作用是可以设//置动画效果。在这里可以看到是对引文部分设置动画//效果。 |
|
$(".quote-text").animate({ |
|
opacity: 0 |
|
}, 500, |
|
function() { |
|
$(this).animate({ |
|
opacity: 1 |
|
}, 500); |
|
$('#text').text(r.quote); |
|
}); |
|
|
|
$(".quote-author").animate({ |
|
opacity: 0 |
|
}, 500, |
|
function() { |
|
$(this).animate({ |
|
opacity: 1 |
|
}, 500); |
|
$('#author').html(r.author); |
|
}); |
|
|
|
var color = Math.floor(Math.random() * colors.length); |
|
$("html body").animate({ |
|
backgroundColor: colors[color], |
|
color: colors[color] |
|
}, 1000); |
|
$(".button").animate({ |
|
backgroundColor: colors[color] |
|
}, 1000); |
|
} |
|
}); |
|
} |
|
$(document).ready(function() { |
|
getQuote(); |
|
$('#new-quote').on('click', getQuote); |
|
$('#tweet-quote').on('click', function() { |
|
if(!inIframe()) { |
|
openURL('https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=' + encodeURIComponent('"' + currentQuote + '" ' + currentAuthor)); |
|
} |
|
}); |
|
$('#tumblr-quote').on('click', function() { |
|
if(!inIframe()) { |
|
openURL('https://www.tumblr.com/widgets/share/tool?posttype=quote&tags=quotes,freecodecamp&caption='+encodeURIComponent(currentAuthor)+'&content=' + encodeURIComponent(currentQuote)); |
|
} |
|
}); |
|
}); |