首页 > web前端 > js教程 > 无法获取隐藏元素宽度和高度如何解决

无法获取隐藏元素宽度和高度如何解决

小云云
发布: 2018-01-23 13:13:49
原创
1854 人浏览过

在实际开发中会遇到确实需要获取隐藏元素的宽高,这儿所说的隐藏元素是display为none的元素。本文主要介绍了无法获取隐藏元素(display:none)宽度(width)和高度(height)的解决方案,具有很好的参考价值。下面跟着小编一起来看下吧,希望能帮助到大家。

可使用jQuery Actual Plugin插件来完成,其源码如下:


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

;( function ( $ ){

 $.fn.addBack = $.fn.addBack || $.fn.andSelf;

 $.fn.extend({

  actual : function ( method, options ){

   // check if the jQuery method exist

   if( !this[ method ]){

    throw '$.actual => The jQuery method "' + method + '" you called does not exist';

   }

   var defaults = {

    absolute   : false,

    clone     : false,

    includeMargin : false,

    display    : 'block'

   };

   var configs = $.extend( defaults, options );

   var $target = this.eq( 0 );

   var fix, restore;

   if( configs.clone === true ){

    fix = function (){

     var style = 'position: absolute !important; top: -1000 !important; ';

     // this is useful with css3pie

     $target = $target.

      clone().

      attr( 'style', style ).

      appendTo( 'body' );

    };

    restore = function (){

     // remove DOM element after getting the width

     $target.remove();

    };

   }else{

    var tmp  = [];

    var style = '';

    var $hidden;

    fix = function (){

     // get all hidden parents

     $hidden = $target.parents().addBack().filter( ':hidden' );

     style  += 'visibility: hidden !important; display: ' + configs.display + ' !important; ';

     if( configs.absolute === true ) style += 'position: absolute !important; ';

     // save the origin style props

     // set the hidden el css to be got the actual value later

     $hidden.each( function (){

      // Save original style. If no style was set, attr() returns undefined

      var $this   = $( this );

      var thisStyle = $this.attr( 'style' );

      tmp.push( thisStyle );

      // Retain as much of the original style as possible, if there is one

      $this.attr( 'style', thisStyle ? thisStyle + ';' + style : style );

     });

    };

    restore = function (){

     // restore origin style values

     $hidden.each( function ( i ){

      var $this = $( this );

      var _tmp = tmp[ i ];

 

      if( _tmp === undefined ){

       $this.removeAttr( 'style' );

      }else{

       $this.attr( 'style', _tmp );

      }

     });

    };

   }

   fix();

   // get the actual value with user specific methed

   // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc

   // configs.includeMargin only works for 'outerWidth' and 'outerHeight'

   var actual = /(outer)/.test( method ) ?

    $target[ method ]( configs.includeMargin ) :

    $target[ method ]();

   restore();

   // IMPORTANT, this plugin only return the value of the first element

   return actual;

  }

 });

})(jQuery);

登录后复制


当然如果要支持模块化开发,直接使用官网下载的文件即可,源码也贴上:


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

;( function ( factory ) {

if ( typeof define === 'function' && define.amd ) {

  // AMD. Register module depending on jQuery using requirejs define.

  define( ['jquery'], factory );

} else {

  // No AMD.

  factory( jQuery );

}

}( function ( $ ){

 $.fn.addBack = $.fn.addBack || $.fn.andSelf;

 $.fn.extend({

  actual : function ( method, options ){

   // check if the jQuery method exist

   if( !this[ method ]){

    throw '$.actual => The jQuery method "' + method + '" you called does not exist';

   }

   var defaults = {

    absolute   : false,

    clone     : false,

    includeMargin : false,

    display    : 'block'

   };

   var configs = $.extend( defaults, options );

   var $target = this.eq( 0 );

   var fix, restore;

   if( configs.clone === true ){

    fix = function (){

     var style = 'position: absolute !important; top: -1000 !important; ';

     // this is useful with css3pie

     $target = $target.

      clone().

      attr( 'style', style ).

      appendTo( 'body' );

    };

    restore = function (){

     // remove DOM element after getting the width

     $target.remove();

    };

   }else{

    var tmp  = [];

    var style = '';

    var $hidden;

    fix = function (){

     // get all hidden parents

     $hidden = $target.parents().addBack().filter( ':hidden' );

     style  += 'visibility: hidden !important; display: ' + configs.display + ' !important; ';

     if( configs.absolute === true ) style += 'position: absolute !important; ';

     // save the origin style props

     // set the hidden el css to be got the actual value later

     $hidden.each( function (){

      // Save original style. If no style was set, attr() returns undefined

      var $this   = $( this );

      var thisStyle = $this.attr( 'style' );

      tmp.push( thisStyle );

      // Retain as much of the original style as possible, if there is one

      $this.attr( 'style', thisStyle ? thisStyle + ';' + style : style );

     });

    };

    restore = function (){

     // restore origin style values

     $hidden.each( function ( i ){

      var $this = $( this );

      var _tmp = tmp[ i ];

 

      if( _tmp === undefined ){

       $this.removeAttr( 'style' );

      }else{

       $this.attr( 'style', _tmp );

      }

     });

    };

   }

   fix();

   // get the actual value with user specific methed

   // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc

   // configs.includeMargin only works for 'outerWidth' and 'outerHeight'

   var actual = /(outer)/.test( method ) ?

    $target[ method ]( configs.includeMargin ) :

    $target[ method ]();

   restore();

   // IMPORTANT, this plugin only return the value of the first element

   return actual;

  }

 });

}));

登录后复制


代码实例:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//get hidden element actual width

$('.hidden').actual('width');

//get hidden element actual innerWidth

$('.hidden').actual('innerWidth');

//get hidden element actual outerWidth

$('.hidden').actual('outerWidth');

//get hidden element actual outerWidth and set the `includeMargin` argument

$('.hidden').actual('outerWidth',{includeMargin:true});

//get hidden element actual height

$('.hidden').actual('height');

//get hidden element actual innerHeight

$('.hidden').actual('innerHeight');

//get hidden element actual outerHeight

$('.hidden').actual('outerHeight');

// get hidden element actual outerHeight and set the `includeMargin` argument

$('.hidden').actual('outerHeight',{includeMargin:true});

//if the page jumps or blinks, pass a attribute '{ absolute : true }'

//be very careful, you might get a wrong result depends on how you makrup your html and css

$('.hidden').actual('height',{absolute:true});

// if you use css3pie with a float element

// for example a rounded corner navigation menu you can also try to pass a attribute '{ clone : true }'

// please see demo/css3pie in action

$('.hidden').actual('width',{clone:true});

登录后复制

相关推荐:

js获取隐藏元素的宽高

js获取隐藏元素宽高的实现方法_javascript技巧

jquery显示隐藏元素的实现代码_jquery


以上是无法获取隐藏元素宽度和高度如何解决的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板