網頁制作poluoluo文章簡介:近來Ed Spencer為一個更具誘惑性網站中的一個工作,這家網站是專賣女性內衣的。 除了不得不一整天去看只穿內衣的女人的圖片這個不值得羨慕的任務之外,Ed Spencer還被迫去寫一個胸罩尺寸計算器。
JavaScript出能能為網頁添加更多互動元素、為網頁的視覺效果錦上添花之外,還能干些什麼?今天這個答案也許會出乎你的意料,外國一位設計師Ed Spencer用JavaScript為一個內衣網站編寫了胸罩罩杯尺寸計算器。我們在使用JavaScript完成日常工作之余,其實也能編寫這種有趣的程序。
只在周末放松一下。
近來Ed Spencer為一個更具誘惑性網站中的一個工作,這家網站是專賣女性內衣的。 除了不得不一整天去看只穿內衣的女人的圖片這個不值得羨慕的任務之外,Ed Spencer還被迫去寫一個胸罩尺寸計算器。
胸罩尺寸計算器背後的理論是有點神秘和神奇了。 讓一個男人或野獸理解它並不容易,所以它是幸運的,Ed Spencer完全不屬於那兩類, 他通過了痛苦和折磨的考驗節省了廣大女性的麻煩。
下面來學習學習這個JavaScript有趣實例吧…
這裡是JS源文件,點擊查看
代碼如下:
var BraCalculator = {
/**
* The string to be returned when the result could not be calculated. Overwrite to change this
*/
unknownString: “Unknown”,
cupSizes: ["A", "B", "C", "D", "DD", "E", "EE", "F", "FF", "G", "GG", "H", "HH",
"J", "JJ", "K", "KK", "L", "LL", "M", "MM", "N", "NN"],
/**
* Returns the correct bra size for given under bust and over bust measurements
* @param {Number} underBust The measurement taken under the bust (in inches)
* @param {Number} overBust The measurement taken over the bust (in inches)
* @return {String} The correct bra size for the given measurements (e.g. 32C, 40DD, etc)
*/
calculateSize: function(underBust, overBust) {
var bandSize = this.calculateBandSize(underBust);
var cupSize = this.calculateCupSize(bandSize, overBust);
if (bandSize && cupSize) {
return bandSize + cupSize;
} else {
return this.unknownString;
};
},
/**
* Calculates the correct band size for a given under bust measurement
* @param {Number} underBust The measurement under the bust
* @return {Number} The correct band size
*/
calculateBandSize: function(underBust) {
var underBust = parseInt(underBust, 10);
return underBust + (underBust % 2) + 2;
},
/**
* Calculates the Cup size required given the band size and the over bust measurement
* @param {Number} bandSize The measured band size (should be an even number)
* @param {Number} overBust The measurement taken over the bust
* @return {String} The appropriate alphabetical cup size
*/
calculateCupSize: function(bandSize, overBust) {
var bandSize = parseInt(bandSize, 10);
var overBust = parseInt(overBust, 10);
var diff = overBust - bandSize;
var result = this.cupSizes[diff];
//return false if we couldn’t lookup a cup size
return result ? result : false;
}
};
點擊這裡使用胸罩罩杯尺寸計算器
英文原文:JavaScript Bra Size Calculator