2-3 页面切换部分的代码封装
本节编程练习不计算学习进度,请电脑登录imooc.com操作

页面切换部分的代码封装

封装,即隐藏对象的属性和实现细节,仅对外公开接口。

封装的目的是增强安全性和简化编程,使用者不必了解具体的实现细节,而只是要通过外部接口,以特定的访问权限来使用类的成员

这个主题案例,我采用的是面向接口编程的写法,简单的说就是将行为封装分布在各个对象中,并让这些对象自己各自负责自己的行为,这也是面向对象设计的一个优点

就拿页面切换的效果来说,在某一时刻,元素A需要让页面进行切换,那么元素A不需要关心页面是怎么切换的,它只能要调用到一个接口方法能让页面切换就行了

基于这样的理论,我们就开始改造一下页面切换的零碎的代码块

页面布局与切换是一个整体,我们想调用切换的时候,并不关心其内部处理的手段,所以我们只需要提供一个可以调用的接口就可以,新建一个Swipe.js文件,内部定义一个Swipe工厂方法,内部会产生一个swipe的滑动对象,暴露了scrollTo的接口

JavaScript中没有抽象类与接口的支持,所以很大程度上实现封装都是靠闭包去模拟

具体我看看右边代码块Swipe.js的实现 与index.html中调用

任务

在31行填入相应代码,通过调用接口让页面滚动

swipe.scrollTo($("#content").width() * 2, 5000);
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  6. <title>慕课七夕主题</title>
  7. <link rel='stylesheet' href='style.css' />
  8. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
  9. <script type="text/javascript" src="Swipe.js"></script>
  10. </head>
  11.  
  12. <body>
  13. <div id='content'>
  14. <ul class='content-wrap'>
  15. <!-- 第一副画面 -->
  16. <li> 页面1 </li>
  17. <!-- 第二副画面 -->
  18. <li> 页面2 </li>
  19. <!-- 第三副画面 -->
  20. <li> 页面3 </li>
  21. </ul>
  22. <div class="button">
  23. <button>点击切换页面</button>
  24. </div>
  25. </div>
  26. <script type="text/javascript">
  27. var swipe = Swipe($("#content"));
  28.  
  29. $('button').click(function() {
  30. // 调用接口
  31. //?
  32. });
  33. </script>
  34. </body>
  35.  
  36. </html>
  1. * {
  2. padding: 0;
  3. margin: 0;
  4. }
  5.  
  6. ul,
  7. li {
  8. list-style-type: none;
  9. }
  10. /*主体部分*/
  11.  
  12. #content {
  13. width: 100%;
  14. height: 100%;
  15. /* top: 20%;
  16.   left: 20%; */
  17. overflow: hidden;
  18. position: absolute;
  19. }
  20.  
  21. .content-wrap {
  22. position: relative;
  23. }
  24.  
  25. .content-wrap > li {
  26. background: #CAE1FF;
  27. color: red;
  28. float: left;
  29. overflow: hidden;
  30. position: relative;
  31. }
  32.  
  33. li:nth-child(2) {
  34. background: #9BCD9B;
  35. }
  36.  
  37. li:nth-child(3) {
  38. background: yellow;
  39. }
  40.  
  41. button {
  42. width: 100px;
  43. height: 50px;
  44. }
  45.  
  46. .button {
  47. position: absolute;
  48. bottom: 0;
  49. }
  1. /////////
  2. //页面滑动 //
  3. /////////
  4.  
  5. /**
  6.  * [Swipe description]
  7.  * @param {[type]} container [页面容器节点]
  8.  * @param {[type]} options [参数]
  9.  */
  10. function Swipe(container) {
  11. //获取第一个子节点
  12. var element = container.find(":first");
  13.  
  14. //滑动对象
  15. var swipe = {};
  16.  
  17. //li页面数量
  18. var slides = element.find("li");
  19.  
  20. //获取容器尺寸
  21. var width = container.width();
  22. var height = container.height();
  23.  
  24. //设置li页面总宽度
  25. element.css({
  26. width: (slides.length * width) + 'px',
  27. height: height + 'px'
  28. });
  29.  
  30. //设置每一个页面li的宽度
  31. $.each(slides, function(index) {
  32. var slide = slides.eq(index); //获取到每一个li元素
  33. slide.css({
  34. width: width + 'px',
  35. height: height + 'px'
  36. });
  37. });
  38.  
  39. //监控完成与移动
  40. swipe.scrollTo = function(x, speed) {
  41. //执行动画移动
  42. element.css({
  43. 'transition-timing-function' : 'linear',
  44. 'transition-duration' : speed + 'ms',
  45. 'transform' : 'translate3d(-' + x + 'px,0px,0px)'
  46. });
  47. return this;
  48. };
  49.  
  50. return swipe;
  51. }
下一节