【自己給自己題目做】之一:橢圓可點擊區域

【題一】
請實現以下需求,要做一個活動頁面,頁面上有一張圖片(假設是800x600),圖片正中心有一個橢圓形的可點擊區域,假設橢圓長軸為200px(橫向),短軸160px(縱向),請實現點擊這個橢圓區域彈出“我被點擊了”的字樣,而其他區域點擊無效。(不一定要兼容低端瀏覽器,能兼容當然更好)

我說這是我曾經出過的一道筆試題。其實主要考察點是基本的數學能力和用web前端相關知識實現需求的綜合能力。難度不算太大。用普通的dom或者canvas來實現都ok,因為其實重要思路是一致的。橢圓區域還是要自己判斷。

先看demo后講思路:

demo:?http://hongru.github.io/quiz/1/index.html

考點主要是以下幾個:

1. 常用dom操作和簡單事件機制(用類庫比如jq也算)
2. 簡單數學知識(橢圓公式,坐標是否在橢圓區域的判斷)
3. 數學模型到編程實踐的簡單轉換

代碼不復雜:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
body{font-family:Microsoft Yahei;
}
.doc {width: 804px;margin: 0 auto;
}
#cont {border: 2px solid #999;height: 600px;position: relative;
}
.dot {position: absolute;width:1px;height: 1px;overflow: hidden;font-size:0;line-height: 0;background: #333;
}
</style>
</head><body><div class="doc"><h4>【題一】</h4><p>
請實現以下需求,要做一個活動頁面,頁面上有一張圖片(假設是800x600),圖片正中心有一個橢圓形的可點擊區域,假設橢圓長軸為200px(橫向),短軸160px(縱向),請實現點擊這個橢圓區域彈出“我被點擊了”的字樣,而其他區域點擊無效。(不一定要兼容低端瀏覽器,能兼容當然更好)</p><div id="cont"></div></div><script>;(function () {var win = window,doc = document,OFFSET;function _bind (el, ev, fn) {return el.addEventListener ? el.addEventListener(ev, fn, false) : el.attachEvent('on'+ev, function () { fn.call(el); });}function _$ (id) {return doc.getElementById(id) || id;}                               function _drawElipse (id, a, b) {var el = _$(id);var docfrag = doc.createDocumentFragment();for (var i = 0; i < 360; i ++) {var dot = doc.createElement('div');dot.className = 'dot';var l = a*Math.sin(i) + (el.offsetWidth - 4)/2,
                        t = b*Math.cos(i) + (el.offsetHeight - 4)/2;
                    dot.style.left = l + 'px';dot.style.top = t + 'px';docfrag.appendChild(dot);}el.appendChild(docfrag);}function offset (el) {var width = el.offsetWidth,height = el.offsetHeight,top = el.offsetTop,left = el.offsetLeft;while (el = el.offsetParent) {top = top + el.offsetTop;left = left + el.offsetLeft;}return {top: top,left: left,height: height,width: width}}function clickCheck (e) {e = e || win.event;var tar = e.target || e.srcElement,x = e.clientX + doc.body.scrollLeft + doc.documentElement.scrollLeft - OFFSET.left - (tar.offsetWidth/2),
                    y = e.clientY + doc.body.scrollTop + doc.documentElement.scrollTop - OFFSET.top - (tar.offsetHeight/2);var r = Math.pow((x/100), 2) + Math.pow((y/80), 2);console && console.log(x, y, r);if (r < 1) {alert('橢圓被點擊了!');}}function __init() {_drawElipse('cont', 100, 80);var el = _$('cont');OFFSET = offset(el);_bind(el, 'click', clickCheck);}__init();})();</script>
</body>
</html>
View Code

?

其實重要的代碼就是以下一段:

 1         function clickCheck (e) {
 2                 e = e || win.event;
 3                 var tar = e.target || e.srcElement,
 4                     x = e.clientX + doc.body.scrollLeft + doc.documentElement.scrollLeft - OFFSET.left - (tar.offsetWidth/2),
 5                     y = e.clientY + doc.body.scrollTop + doc.documentElement.scrollTop - OFFSET.top - (tar.offsetHeight/2);
 6                 
 7                 var r = Math.pow((x/100), 2) + Math.pow((y/80), 2);
 8                 console && console.log(x, y, r);
 9                 if (r < 1) {
10                     alert('橢圓被點擊了!');
11                 }
12             }    

因為橢圓是畫在中心的,上面的4,5行,獲取x,y其實就是獲取當前鼠標位置相對于容器中心的相對距離。(要算上scroll的距離和容器本身距離頁面邊緣的位置)

然后用橢圓公式,如果這個值小于1,那么表示在橢圓內點擊的。

結束。

-------------------------------------

下期:定寬容器內若干大小不定圖片自動排列的問題,允許一定程度內的縮放和裁剪,類似于下面的結果:

轉載于:https://www.cnblogs.com/hongru/p/3187934.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274162.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274162.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274162.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

軟件設計師中級 百度知道_設計師應該知道什么

軟件設計師中級 百度知道… and no, it doesn’t have to be how to code.……而且&#xff0c;不必一定要編碼。 Here are a few points that have helped me assess what technical knowledge is necessary for designers to be successful.以下幾點幫助我評估了設計師成功所…

看看清華的同學在四年的大學中干什么吧,非常值得學習

&#xff08;一&#xff09; 永遠不要說你已經盡力了 我在高中時體育特別差&#xff0c;跑1000米都很要命&#xff0c;從來都是不及格。到了清華之后&#xff0c;第一節體育課&#xff0c;老師告訴我們每年要測3000米長跑&#xff0c;跑不過不許畢業&#xff0c;取消推研資格。…

信息保真度準則_設計保真度的新的非科學公式

信息保真度準則As designers, our audience is more than just our users. We keep our user’s needs top of mind while designing for their jobs-to-be-done, but sometimes we need to illustrate the problem and articulate solutions to a broader audience: our cross…

zend studio配置調試(Xdebug方式)

1.下載xdebug http://xdebug.org/download.php 我下的是PHP 5.4 VC9 (32 bit) 【當前系統php是php5.4.14(win32)版本】 2.配置php.ini 在文件最后配置 [Xdebug]zend_extension "C:/php5.4.14/ext/php_xdebug-2.2.3-5.4-vc9.dll" xdebug.auto_trace1xdebug.collect_p…

人物肖像速寫_肖像學的基礎

人物肖像速寫More in the iconography series:? 7 Principles of Icon Design? 5 Ways to Create a Settings Icon? Icon Grids & Keylines Demystified? Pixel-Snapping in Icon Design? 3 Classic Icon FamiliesAn icon is a compact symbol that represents a disc…

python處理網絡文字流,設置為utf8編碼

import sysreload(sys)sys.setdefaultencoding(utf8)在python編程時&#xff0c;如果數據來自客戶端傳過來&#xff0c;在使用"%s" % str和json.loads(str)時&#xff0c;會遇到問題。問題的原因是字符集和編碼的問題。以上代碼可以解決這個問題。 轉載于:https://ww…

產品設計的Kawaiization

重點 (Top highlight)在過去的一兩年中&#xff0c;我注意到品牌和產品設計中出現了某種風格。 (Over the last year or two, I’ve noticed a certain style emerge in brand and product design.) (this article originally appeared on DESK magazine)(本文最初出現在DESK雜…

寫了兩個簡單的小工具,文件夾文件操作的

一&#xff0c;文件夾A下的文件夾下的文件&#xff0c;移到文件夾A下。 二&#xff0c;經常上貼吧什么的&#xff0c;有些圖貼&#xff0c;會直接網頁全部保存為&#xff0c;結果是一個html文件&#xff0c;和引用資源的文件夾&#xff0c;文件夾下有很多無用的文件&#xff0c…

前置聲明相關

前置聲明相關 一個前置聲明是指在程序員尚未給出完整定義之前對一個標示符(一個類型、一個變量或者一個函數)的聲明。一個很簡單的例子就是我們在函數A中使用了函數B&#xff0c;但是函數B的聲明在函數A之后&#xff0c;這個時候&#xff0c;就需要對函數B進行前置聲明&#xf…

陌生人社會_陌生人之旅

陌生人社會The Last of Us Part II is a game that is deeply invested in the minutiae of its characters. The pain they cause, the things that drive them, and the particularities of their self-destruction and salvation. The game’s commitment to the true natur…

設計師更高效_要成為更好的設計師,我們需要更多的時間進行游戲

設計師更高效重點 (Top highlight)I’m a busy designer. I’m fortunate to be booked out months in advance. My freelance career has proven more stable than other’s “jobs”. I don’t wear busy as a badge of honour. I don’t condone hustle culture or compare …

java數據類型及其說明

Java數據類型及其說明java基本數據類型&#xff1a; java數據類型分為基本數據類型和引用數據類型&#xff0c;基本數據類型就是4類8種&#xff0c;分為數值類型&#xff08;整數型[byte&#xff0c;short&#xff0c;int&#xff0c;long]&#xff09;&#xff0c;字符型&…

ux設計師薪水_公司與 設計機構:UX設計師的津貼和陷阱

ux設計師薪水Written by Yegor Tsynkevich由Yegor Tsynkevich撰寫 The more companies understand the power of a great user experience design and its impact on customer loyalty, the more they are willing to have it embedded in their culture. With so much emphas…

ZOJ 2165 Red and Black

1.采用dfs&#xff1a; #include <iostream>#include <cstdio>#include <cstring>#include <string>using namespace std;char map[25][25];int count 1;int r,c;int dx[4] {1,-1,0,0};int dy[4] {0,0,1,-1};bool judge(int x,int y){ if(x<0…

java中的equals用法

在Object 類中定義有&#xff1a; 1、public boolean equals(Object object )方法提供定義對象是否“相等”邏輯。 2、Object的equals方法定義為&#xff1a;x.equals(y)當x和y是同一個對象的引用時&#xff0c;返回true&#xff0c;否則返回false 3、在其他一些類中&#xff0…

根據圖片獲得配色方案_配色系列(1)—從圖片中獲得配色靈感

根據圖片獲得配色方案前言 (Foreword) When we start designing mobile web pages, we always need to determine the color scheme of the web page first. Well, at this time, unless the customer proposes a color scheme, most of the situations we have to face is to …

七大查找算法

1. 順序查找2. 二分查找3. 插值查找4. 斐波那契查找5. 樹表查找6. 分塊查找7. 哈希查找 查找是在大量的信息中尋找一個特定的信息元素&#xff0c;在計算機應用中&#xff0c;查找是常用的基本運算&#xff0c;例如編譯程序中符號表的查找。本文簡單概括性的介紹了常見的七種查…

HA2795Billboard 可用線段樹

#include<iostream>using namespace std;#include<cstdio>#include<algorithm>#define maxn 200005int h,n,w;int root[maxn<<4];int ans;//標記void make_tree(int l,int r,int rt){if(lr){root[rt]w;return ;}int mid(rl)/2;make_tree(l,mid,rt*2);m…

axure下拉列表框單選框_如何在Axure中創建下拉菜單和組合框

axure下拉列表框單選框First, let’s clarify what exactly is a dropdown menu, and what is a combo box, aren’t they the same? Well … no, not really, let me explain.首先&#xff0c;讓我們弄清楚什么是下拉菜單&#xff0c;什么是組合框&#xff0c;不是嗎&#xf…

Android 第一課 Activity

以下說明基于Android Studio&#xff0c;簡稱AS。&#xff08;紅色字體為自行添加&#xff0c;注在醒目&#xff09; 具體包括&#xff1a;創建活動創建項目 加載布局 在AndroidManifest文件中注冊 活動中使用&#xff08;提醒&#xff09;Toast 在活動使用&#xff08;菜…