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

【题一】
请实现以下需求,要做一个活动页面,页面上有一张图片(假设是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,一经查实,立即删除!

相关文章

信息保真度准则_设计保真度的新的非科学公式

信息保真度准则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…

产品设计的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杂…

陌生人社会_陌生人之旅

陌生人社会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…

java数据类型及其说明

Java数据类型及其说明java基本数据类型&#xff1a; java数据类型分为基本数据类型和引用数据类型&#xff0c;基本数据类型就是4类8种&#xff0c;分为数值类型&#xff08;整数型[byte&#xff0c;short&#xff0c;int&#xff0c;long]&#xff09;&#xff0c;字符型&…

根据图片获得配色方案_配色系列(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;菜…

figma设计_一种在Figma中跟踪设计迭代的简单方法

figma设计As designers, telling a good story is always part of the job. A great story engages the client with the journey of decision making; it shows your team the breadth and depth of the exploration; it also encourages us to reflect on our own design pro…

latex 插图 上下放_专辑插图中上下文中的文本

latex 插图 上下放Especially if, like me, you’re not properly educated in the world of visual design, typography, and all those other things that a formal education can bring. We’re kind of playing around until something fits right, and doesn’t feel jarr…

视觉感知_产品设计中的视觉感知

视觉感知The role of the UX designer has evolved immensely over time, but at its core, it remains the same- UX设计人员的角色随着时间的流逝而发生了巨大的变化&#xff0c;但从本质上讲&#xff0c;它保持不变- to deliver information to users in an effective mann…

pb 插入报列在此处不_获取有关[在此处插入问题]的事实

pb 插入报列在此处不Twitter’s recent move to put notices on tweets themselves is one of the most controversial social media features during our times. As a design technologist, I can’t help but wonder the decision-making process behind it. It’s a perfect…

设计模式_单实体模式

Singleton 三要素&#xff1a;private 构造函数、 public 静态方法、 public 静态变量 单实例模式的三种线程安全实现方式&#xff08;&#xff23;&#xff0b;&#xff0b;&#xff09; &#xff11; 懒汉模式&#xff1a;即第一次调用该类实例的时候才产生一个新的该类实例…

c++编写托管dll_教程:如何编写简单的网站并免费托管

c编写托管dll本教程适用于谁&#xff1f; (Who is this tutorial for?) This tutorial assumes no prior knowledge and is suitable for complete beginners as a first project 本教程假定您没有先验知识&#xff0c;并且适合初学者作为第一个项目 您将需要什么 (What you w…

浅述WinForm多线程编程与Control.Invoke的应用

在WinForm开发中&#xff0c;我们通常不希望当窗体上点了某个按钮执行某个业务的时候&#xff0c;窗体就被卡死了&#xff0c;直到该业务执行完毕后才缓过来。一个最直接的方法便是使用多线程。多线程编程的方式在WinForm开发中必不可少。本文介绍在WinForm开发中如何使用多线程…

设计 色彩 构图 创意_我们可以从时尚的创意方向中学到色彩

设计 色彩 构图 创意The application of fashion as a form of aesthetic expression is a notion familiar to many. Every day, we curate ourselves with inspiration from rising trends, a perception of our personal preferences, and regards to practicality in the c…

Android 第六课 4种基本布局之LinearLayout和Relativelayout

看完控件&#xff0c;紧接着看布局&#xff0c;布局是可以来放置控件&#xff0c;管理控件的。布局里也可以嵌套布局。我们新建项目UILayoutTest项目&#xff0c;活动名和布局名选择默认。加入活动及其对应的布局已经创建完成。线性布局(LinearLayout)android:layout_gravity属…