【自己给自己题目做】之一:椭圆可点击区域

【题一】
请实现以下需求,要做一个活动页面,页面上有一张图片(假设是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.mzph.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;菜…