为了账号安全,请及时绑定邮箱和手机立即绑定

变换比例时如何检查元素是否在视口外

变换比例时如何检查元素是否在视口外

凤凰求蛊 2021-12-02 15:00:59
我知道getBoundingClientRect()在视图中有边界,但我发现如果视图有变换比例,这将不起作用。您的任何解决方案。var bounding = elem.getBoundingClientRect();// Check if it's out of the viewport on each sidevar out = {};out.top = bounding.top >= 0;out.left = bounding.left >= 0;out.bottom = (bounding.bottom) > ((window.innerHeight) || (document.documentElement.clientHeight));out.right = (bounding.right) > ((window.innerWidth) || (document.documentElement.clientWidth));
查看完整描述

1 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

请拖动方块,它得到了变换比例和旋转。


$.fn.isOnScreen = function(partial){


    //let's be sure we're checking only one element (in case function is called on set)

    var t = $(this).first();


    //we're using getBoundingClientRect to get position of element relative to viewport

    //so we dont need to care about scroll position

    var box = t[0].getBoundingClientRect();


    //let's save window size

    var win = {

        h : $(window).height(),

        w : $(window).width()

    };


    //now we check against edges of element


    //firstly we check one axis

    //for example we check if left edge of element is between left and right edge of scree (still might be above/below)

    var topEdgeInRange = box.top >= 0 && box.top <= win.h;

    var bottomEdgeInRange = box.bottom >= 0 && box.bottom <= win.h;


    var leftEdgeInRange = box.left >= 0 && box.left <= win.w;

    var rightEdgeInRange = box.right >= 0 && box.right <= win.w;



    //here we check if element is bigger then window and 'covers' the screen in given axis

    var coverScreenHorizontally = box.left <= 0 && box.right >= win.w;

    var coverScreenVertically = box.top <= 0 && box.bottom >= win.h;


    //now we check 2nd axis

    var topEdgeInScreen = topEdgeInRange && ( leftEdgeInRange || rightEdgeInRange || coverScreenHorizontally );

    var bottomEdgeInScreen = bottomEdgeInRange && ( leftEdgeInRange || rightEdgeInRange || coverScreenHorizontally );


    var leftEdgeInScreen = leftEdgeInRange && ( topEdgeInRange || bottomEdgeInRange || coverScreenVertically );

    var rightEdgeInScreen = rightEdgeInRange && ( topEdgeInRange || bottomEdgeInRange || coverScreenVertically );


    //now knowing presence of each edge on screen, we check if element is partially or entirely present on screen

    var isPartiallyOnScreen = topEdgeInScreen || bottomEdgeInScreen || leftEdgeInScreen || rightEdgeInScreen;

    var isEntirelyOnScreen = topEdgeInScreen && bottomEdgeInScreen && leftEdgeInScreen && rightEdgeInScreen;


    return partial ? isPartiallyOnScreen : isEntirelyOnScreen;


};


$.expr.filters.onscreen = function(elem) {

  return $(elem).isOnScreen(true);

};


$.expr.filters.entireonscreen = function(elem) {

  return $(elem).isOnScreen(true);

};


$(function(){


$('#circle1').draggable({   drag: function() {

if ($("#circle1").isOnScreen()) $('.indicator').html('yes its on screen');

else $('.indicator').html('its off screen');

      },});



});

.circle{

width: 50px;

height: 50px;

background-color: red;

top: 50%;

left: 50%;

}


.circle.big{

transform: scale(2,2) rotate(20deg);


}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>

<script

  src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"

  integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="

  crossorigin="anonymous"></script>

  

<div class="circle big" id="circle1">drag me</div>


<div class="indicator"></div>

Press to enlarge


查看完整回答
反对 回复 2021-12-02
  • 1 回答
  • 0 关注
  • 141 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信