JavaScript面向對象分層思惟全面解析

這篇文章主要介紹了JavaScript面向對象分層思惟,js自己不是面嚮對象語言,在咱們實際開發中其實不多用到面向對象思想。下文關於js面向對象知識給你們介紹的很是詳細,感興趣的朋友一塊兒看看吧javascript

js自己不是面嚮對象語言,在咱們實際開發中其實不多用到面向對象思想,之前一直覺得當要複用的時候才封裝成對象,然而隨着如今作的項目都後期測試階段發現面向對象的做用不單單只是複用,可能大家會說面向對象還有繼承,多態的概念,但在javascript裏面多態的概念是不存在,而繼承因爲web頁面的必須先下載js在運行致使js的繼承不能像後臺那麼靈活並且js沒有重載以及重寫不方便(並且js中重寫的意義不是很大),因此在js中不多用到面向對象,可能在一些插件中會看到對象的寫法,寫js的都會有一樣的感受在寫一個插件的時候通常是先用面相過程把插件功能寫出來,而後在重構改爲對象的方法.但在實際項目開發中要求時間進度和開發成本不多會有那麼寬鬆的時間讓你先用面向過程實現功能在重構.實際開發中咱們基本都是用面相過程寫完就直接提交了.css

這種寫法發現一個問題就是,當你把這個頁面的寫完了以後過一段時間忽然這個頁面的功能需求或是頁面佈局要調整,你在看這個頁面的代碼,一下很難快速的把整個頁面的代碼邏輯步驟梳理清楚,我相信不少寫前端都要同感吧! 舉個例子:我幾年前寫的放大鏡插件,如今我在放出來發現我寫的這個插件在谷歌瀏覽器第一次渲染的時候沒有效果,代碼以下:html

/// <reference path="../jquery11.js" />前端

(function ($) {java

$.fn.extend({jquery

jqoom: function (potions) {web

var settings = {瀏覽器

width: 350,app

height: 350,ide

position: "right"

}

if (potions) {

$.extend(settings, potions);

}

var ImgUrl = $("img", this).attr("src");

var ImgMinWidth = $("img", this).width();

var ImgMinHeigth = $("img", this).height();

var ImgWidth = 0;

var ImgHeight = 0;

var de = true;

$(this).hover(function (e) {

}, function () {

$("#jqoomz").remove();

$(document).unbind("mousemove");

$("#jqoomy").remove();

de = true;

});

$("img", this).hover(function (e) {

var pagex = e.x || e.pageX;

var pagey = e.y || e.pageY;

var pagex1 = 0;

var pagey1 = 0;

var leftcha = 0;

var topcha = 0;

_this = $(this).parents("div");

if ($("#jqoomz").length == 0) {

_this.after("<div id='jqoomz'></div>");

var obj = new Image();

obj.src = ImgUrl;

obj.onload = function () {

if (de && obj.height > 0) {

de = false;

ImgWidth = obj.width;

ImgHeight = obj.height;

finder.call(_this.find("img")[0]);

}

};

$("#jqoomz").width(settings.width).height(settings.height).offset({

left: $(_this).outerWidth() + $(_this).offset().left,

top: $(_this)[0].offsetTop

}).append($("<img></img>").attr("src", ImgUrl));

if (de && obj.height > 0) {

de = false;

ImgWidth = obj.width;

ImgHeight = obj.height;

finder.call(this);

}

}

function mover(event) {

var pagex2 = event.x || event.pageX;

var pagey2 = event.y || event.pageY;

if (parseInt(pagex2 + leftcha) <= parseInt($(_this).width() + $(_this).offset().left) && pagex2 >= leftcha + $(_this).offset().left) {

$(this).offset({left: pagex2 - leftcha});

} else {

if (parseInt(pagex2 + leftcha) > parseInt($(_this).width() + $(_this).offset().left) && pagex2)

$(this).offset({left: $(_this).width() + $(_this).offset().left - leftcha * 2});

else

$(this).offset({left: $(_this).offset().left});

}

if (parseInt(pagey2 + topcha) <= parseInt($(_this).height() + $(_this).offset().top) && pagey2 >= topcha + $(_this).offset().top) {

$(this).offset({top: (pagey2 - topcha)});

//document.getElementById("move").style.top = (pagey2 - (this.pagey - this.divtop)).toString() + "px";

} else {

if (parseInt(pagey2 + topcha) > parseInt($(_this).height() + $(_this).offset().top))

$(this).offset({top: ($(_this).height() + $(_this).offset().top - topcha * 2)});

//document.getElementById("move").style.top = (this.height - this.divHeight).toString() + "px";

else

$(this).offset({top: $(_this).offset().top});

//document.getElementById("move").style.top = "0px"

}

var bilx = ($(this).offset().left - $(_this).offset().left) / (ImgMinWidth / ImgWidth);

var bily = ($(this).offset().top - $(_this).offset().top) / (ImgMinHeigth / ImgHeight);

$("#jqoomz img").css({"margin-left": -bilx, "margin-top": -bily});

}

function finder() {

if (parseFloat($(this).offset().top + $(this).height() - (ImgMinHeigth / ImgHeight * ImgMinHeigth)) >=

parseFloat(pagey - ImgMinHeigth / ImgHeight * ImgMinHeigth / 2) && parseFloat(pagey - ImgMinHeigth / ImgHeight * ImgMinHeigth / 2) >=

parseFloat($(this).offset().top)) {

pagey1 = (pagey - ImgMinHeigth / ImgHeight * ImgMinHeigth / 2);

} else {

if ((pagey - ImgMinHeigth / ImgHeight * ImgMinHeigth / 2) < $(this).offset().top) {

pagey1 = $(this).offset().top;

} else {

pagey1 = ($(this).offset().top + $(this).height() - (ImgMinHeigth / ImgHeight * ImgMinHeigth));

}

}

if (($(this).offset().left + $(this).width() - ImgMinWidth / ImgWidth * ImgMinWidth) >=

(pagex - ImgMinWidth / ImgWidth * ImgMinWidth / 2) && (pagex - ImgMinWidth / ImgWidth * ImgMinWidth / 2) >=

$(this).offset().left) {

pagex1 = (pagex - ImgMinWidth / ImgWidth * ImgMinWidth / 2);

} else {

if ((pagex - ImgMinWidth / ImgWidth * ImgMinWidth / 2) < $(this).offset().left) {

pagex1 = $(this).offset().left;

} else {

pagex1 = ($(this).offset().left + $(this).width() - ImgMinWidth / ImgWidth * ImgMinWidth);

}

}

leftcha = ImgMinWidth / ImgWidth * ImgMinWidth / 2;

topcha = ImgMinHeigth / ImgHeight * ImgMinHeigth / 2;

if ($("#jqoomy").length == 0) {

$(this).after("<div id='jqoomy'></div>")

.siblings("#jqoomy")

.addClass("jqoomy").show()

.width((ImgMinWidth / ImgWidth * ImgMinWidth))

.height((ImgMinHeigth / ImgHeight * ImgMinHeigth)).offset({

top: pagey1,

left: pagex1

});

}

$(document).on("mousemove", $.proxy(mover, $("#jqoomy")));

}

}, function () {

});

}

});

})(jQuery);

html:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

<script src="jquery11.js"></script>

<script src="OppJqoom.js"></script>

<style type="text/css">

.jqoom

{

width: 350px;

height: 350px;

border: solid 1px #DFDFDF;

z-index: 10;

}

.jqoom img

{

cursor: pointer;

z-index: 10;

max-height: 350px;

max-width: 350px;

}

.jqoomy

{

background-color: white;

position: relative;

z-index: 999;

opacity: 0.5;

cursor: pointer;

border: solid 1px #DFDFDF;

}

#jqoomz

{

border: solid 1px #DFDFDF;

position: absolute;

overflow: hidden;

}

.lef

{

border: 1px solid #DFDFDF;

display: block;

height: 72px;

line-height: 72px;

text-align: center;

text-decoration: none;

width: 10px;

background-color:#EBEBEB;

float:left;

}

.lef:hover

{

color:red;

}

.jqooz

{

float:left;

width:352px;

margin-top:20px;

}

.jqooz ul

{

float: left;

margin: 0;

padding: 0;

width:328px;

height:72px;

}

.jqooz ul li

{

display: inline;

list-style: none outside none;

margin: 0 10px;

}

.jqooz ul li img

{

border: 1px solid #DFDFDF;

max-height: 72px;

max-width: 120px;

}

.jqooz ul li img:hover

{

border: 1px solid #ff6600;

}

</style>

<script type="text/javascript">

$(function () {

$(".jqoom").jqoom();

});

</script>

</head>

<body>

<div class="jqoom">

<img src="b3.jpg" />

</div>

<div class="jqooz">

<a href="javascript:void(0)" class="lef"><</a>

<ul>

<li><a>

<img src="b3.jpg" /></a></li>

</ul>

<a href="javascript:void(0)" class="lef">></a>

</div>

</body>

</html>

相關文章
相關標籤/搜索