基于JavaScript实现移动端点击图片查看大图点击大图隐藏


Posted in Javascript onNovember 04, 2015

一、需求

点击图片查看大图,再点大图隐藏。多用于移动端,因为移动端屏幕小,可能需要查看大图。

二、代码

<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head runat="server">
<title>JQuery点击图片查看大图by starof</title>
<style type="text/css">
.exampleImg { height:100px; cursor:pointer;}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
//alert($);
// (function (window, undefined) {
// var MyJQuery = function () {
// window.MyjQuery = window.$ = jQuery; window.$ = MyJQuery;
// };
// })(window);
// alert($);
$.fn.ImgZoomIn = function () {
bgstr = '<div id="ImgZoomInBG" style=" background:#000000; filter:Alpha(Opacity=70); opacity:0.7; position:fixed; left:0; top:0; z-index:10000; width:100%; height:100%; display:none;"><iframe src="about:blank" frameborder="5px" scrolling="yes" style="width:100%; height:100%;"></iframe></div>';
//alert($(this).attr('src'));
imgstr = '<img id="ImgZoomInImage" src="' + $(this).attr('src')+'" onclick=$(\'#ImgZoomInImage\').hide();$(\'#ImgZoomInBG\').hide(); style="cursor:pointer; display:none; position:absolute; z-index:10001;" />';
if ($('#ImgZoomInBG').length < 1) {
$('body').append(bgstr);
}
if ($('#ImgZoomInImage').length < 1) {
$('body').append(imgstr);
}
else {
$('#ImgZoomInImage').attr('src', $(this).attr('src'));
}
//alert($(window).scrollLeft());
//alert( $(window).scrollTop());
$('#ImgZoomInImage').css('left', $(window).scrollLeft() + ($(window).width() - $('#ImgZoomInImage').width()) / 2);
$('#ImgZoomInImage').css('top', $(window).scrollTop() + ($(window).height() - $('#ImgZoomInImage').height()) / 2);
$('#ImgZoomInBG').show();
$('#ImgZoomInImage').show();
};
$(document).ready(function () {
$("#imgTest").bind("click", function () {
$(this).ImgZoomIn();
});
});
</script>
</head>
<body>
<div>
<!--第一种写法-->
<img class="exampleImg" src="images/03.jpg" id="imgTest"/>
<!--第二种写法-->
<img class="exampleImg" src="images/p1_nav2.png" onClick="$(this).ImgZoomIn();"/>
</div>
</body>
</html>

三、技巧

因为移动端无法添加热点,最终一个解决方法是使用四个a标签定位到左上角,右上角,左下角,右下角四个区域。

<dl>
 <dd style="display:block;">
  <img src="images/four-duche.jpg" onClick="$(this).ImgZoomIn();">
  <a href="javascript:;" src="images/11.jpg" class="topleft" onClick="$(this).ImgZoomIn();"></a>
  <a href="javascript:;" src="images/12.jpg" class="topright" onClick="$(this).ImgZoomIn();"></a>
  <a href="javascript:;" src="images/13.jpg" class="bottomleft" onClick="$(this).ImgZoomIn();"></a>
  <a href="javascript:;" src="images/14.jpg" class="bottomright" onClick="$(this).ImgZoomIn();"></a>
 </dd>
 ...
</dl>
css
.topleft,.topright,.bottomleft,.bottomright{
 width:50%;
 height:50%;
 position:absolute;
}
.topleft{
 /*background-color:red;*/
 top:0;
 left:0;
}
.topright{
 /*background-color:green;*/
 top:0;
 right:0;
}
.bottomleft{
 /*background-color:blue;*/
 bottom:0;
 left:0;
}
.bottomright{
 /*background-color:yellow;*/
 bottom:0;
 right:0;
}

PS:手机网站移动端图片实现延迟加载

由于国内的电信网络性价比的限制,和手机处理能力的差异,在设计一个无线应用的时候,

为用户节省流量是一个非常重要的考虑因素。可以说每一个字节都应该为客户端节省。

节约流量可以从以下几个方面关注:

一、使用缓存 比如 利用浏览器本地存储 前面已经讨论过

二、延迟加载代码 (触底检测,通过接口获取数据)

三、资源的延迟加载,图片出现在可视区域再加载,(不考虑自动播放的情况下)音频视频按用户点击加载。

今天简单说一下图片延迟加载的实现方式。

例子基于jQuery 和 jQuery mobile

原理:用户滑动屏幕,屏幕滚动结束(用jQuery 提供的 window scrollstop 事件合适 ) 检测出现在viewport中的图片。

替换图片的 真正 src 属性即可。

技巧:滚动结束之后不要立即检测加载,设置一秒延时,也许用户会立即开始下一次滚屏,基于现在的网络环境,1秒的延时可以说明用户真正想查看这些内容。用微信的朋友可以仔细体验一下这一点。

由于有时钟的控制,当用户频繁快速翻动屏幕,不会发大量请求。

主要代码:

var refreshTimer = null,
 mebook = mebook || {};
/*
*滚动结束 屏幕静止一秒后检测哪些图片出现在viewport中
*和PC端不同 由于无线速度限制 和手机运算能力的差异 1秒钟的延迟对手机端的用户来说可以忍受
*/
$(window).on('scrollstop', function () {
 if (refreshTimer) {
 clearTimeout(refreshTimer);
 refreshTimer = null;
 }
 refreshTimer = setTimeout(refreshAll, 1e3);
});
$.belowthefold = function (element) {
  var fold = $(window).height() + $(window).scrollTop();
  return fold <= $(element).offset().top;
};
$.abovethetop = function (element) {
  var top = $(window).scrollTop();
  return top >= $(element).offset().top + $(element).height();
};
/*
*判断元素是否出现在viewport中 依赖于上两个扩展方法 
*/
$.inViewport = function (element) {
  return !$.belowthefold(element) && !$.abovethetop(element)
};
mebook.getInViewportList = function () {
  var list = $('#bookList li'),
    ret = [];
  list.each(function (i) {
    var li = list.eq(i);
    if ($.inViewport(li)) {
      mebook.loadImg(li);
    }
  });
};
mebook.loadImg = function (li) {
  if (li.find('img[_src]').length) {
    var img = li.find('img[_src]'),
      src = img.attr('_src');
    img.attr('src', src).load(function () {
      img.removeAttr('_src');
    });
  }
};
Javascript 相关文章推荐
javascript xml为数据源的下拉框控件
Jul 07 Javascript
jQuery 使用手册(五)
Sep 23 Javascript
jquery 选择器部分整理
Oct 28 Javascript
当鼠标移动到图片上时跟随鼠标显示放大的图片效果
Jun 06 Javascript
jQuery对象与DOM对象转换方法详解
May 10 Javascript
js实现5秒倒计时重新发送短信功能
Feb 05 Javascript
为你的微信小程序体积瘦身详解
May 20 Javascript
vue2 前端搜索实现示例
Feb 26 Javascript
微信公众号平台接口开发 获取access_token过程解析
Aug 14 Javascript
node.js中stream流中可读流和可写流的实现与使用方法实例分析
Feb 13 Javascript
完美解决vue 中多个echarts图表自适应的问题
Jul 19 Javascript
js+css实现扇形导航效果
Aug 18 Javascript
js数组去重的5种算法实现
Nov 04 #Javascript
解决js图片加载时出现404的问题
Nov 30 #Javascript
jquery实现的点击翻书效果代码
Nov 04 #Javascript
JS实现获取键盘按下的按键并显示在页面上的方法
Nov 04 #Javascript
JS实现超精简的链接列表在固定区域内滚动效果代码
Nov 04 #Javascript
JS实现网页上随滚动条滚动的层效果代码
Nov 04 #Javascript
jQuery实现带有动画效果的回到顶部和底部代码
Nov 04 #Javascript
You might like
PHP中PDO基础教程 入门级
2011/09/04 PHP
如何使用php输出时间格式
2013/08/31 PHP
php使用curl简单抓取远程url的方法
2015/03/13 PHP
php实现的生成排列算法示例
2019/07/25 PHP
Javascript----文件操作
2007/01/18 Javascript
js 鼠标拖动对象 可让任何div实现拖动效果
2009/11/09 Javascript
jQuery 定时局部刷新(setInterval)
2010/11/19 Javascript
前端必备神器 Snap.svg 弹动效果
2014/11/10 Javascript
Flash图片上传组件 swfupload使用指南
2015/03/14 Javascript
jquery UI Datepicker时间控件的使用方法(终结版)
2015/11/07 Javascript
jQuery 1.9.1源码分析系列(十)事件系统之主动触发事件和模拟冒泡处理
2015/11/24 Javascript
javascript 继承学习心得总结
2016/03/17 Javascript
jQuery实现邮箱下拉列表自动补全功能
2016/09/08 Javascript
jQuery实现字符串全部替换的方法
2016/12/12 Javascript
JS集合set类的实现与使用方法示例
2019/02/01 Javascript
详解vue项目中调用百度地图API使用方法
2019/04/25 Javascript
Vue.js自定义指令学习使用详解
2019/10/19 Javascript
如何在JS文件中获取Vue组件
2020/09/16 Javascript
[01:27:43]VGJ.S vs TNC Supermajor 败者组 BO3 第三场 6.6
2018/06/07 DOTA
Python编程中用close()方法关闭文件的教程
2015/05/24 Python
python实现web方式logview的方法
2015/08/10 Python
Python中的descriptor描述器简明使用指南
2016/06/02 Python
Python基于opencv的图像压缩算法实例分析
2018/05/03 Python
python装饰器简介---这一篇也许就够了(推荐)
2019/04/01 Python
python 字段拆分详解
2019/12/17 Python
python与c语言的语法有哪些不一样的
2020/09/13 Python
Html5让容器充满屏幕高度或自适应剩余高度的布局实现
2020/05/14 HTML / CSS
Boden澳大利亚官网:英国在线服装公司
2018/08/05 全球购物
Expedia法国:全球最大在线旅游公司
2018/09/30 全球购物
英国健身仓库:Bodybuilding Warehouse
2019/03/06 全球购物
DC Shoes澳大利亚官方网上商店:购买DC鞋子
2019/10/25 全球购物
杭州SQL浙江浙大网新恩普软件有限公司
2013/07/27 面试题
宾馆总经理岗位职责
2014/02/14 职场文书
心理学专业大学生职业生涯规划范文
2014/02/19 职场文书
《找不到快乐的波斯猫》教学反思
2014/02/24 职场文书
利用python做表格数据处理
2021/04/13 Python