简体   繁体   English

无法计算悬停元素的确切位置

[英]Unable to calculate the hover element's exact position

I am just trying to get the mouse hover div's position at the right according to the space around. 我只是想根据周围的空间在右侧将鼠标悬停div的位置。 Somehow I am able to do this in first two columns but not for other columns. 不知何故,我能够在前两列中做到这一点,但对于其他列则无法做到。 May be my calculations while writing the condition state are wrong. 可能是我写条件状态时的计算错误。

Can anyone please help? 谁能帮忙吗?

JS Fiddle URL: JS小提琴网址:

http://jsfiddle.net/mufeedahmad/2u1zr11f/7/ http://jsfiddle.net/mufeedahmad/2u1zr11f/7/

JS Code: JS代码:

 $('.thumb-over-team li').find('.cover').css({opacity:0});

        $('.thumb-over-team li').on('mouseenter', function(){    

                var $this = $(this), 
                    thisoffset = $this.position().left,
                    openDivId = $(this).find('.cover'),
                    thumbContainer = '.thumb-over-team',
                    speedanim = 200;


                 if(thisoffset + openDivId.outerWidth() >= $(thumbContainer).outerWidth()){
                     //thisoffset = $(thumbContainer).outerWidth() - openDivId.outerWidth() - 212;
                    thisoffset = thisoffset - openDivId.outerWidth()+10;                     
                     openDivId.stop().css({'z-index':'9999'}).animate({'opacity':'1', 'left':-thisoffset}, 200);
                     }else{                      
                         openDivId.stop().css({'z-index':'9999'}).animate({'opacity':'1', 'left':'212px'}, 200); 
                     }






        }).on('mouseleave', function(){         
            $(this).find('.cover').stop().css({'z-index':'-1'}).animate({'opacity':'0', 'left':'200px'}, 200);

        });

        $('.close-parent').on('click', function(){          
            $(this).parents('.cover').stop().css({'z-index':'-1'}).animate({'opacity':'0', 'left':'200px'}, 200);

        });;

In your first conditional, try to calculate the position of the offset as: 在第一个条件中,尝试按以下方式计算偏移量的位置:

thisoffset = ($(thumbContainer).outerWidth() - openDivId.outerWidth() - thisoffset);

That way, you're adjusting the appearing square (.cover) when it doesn't fit inside the container, to be as close possible to its rightmost edge: (maximum width - appearing square width - current li position) 这样,您正在调整出现的正方形(.cover)使其不适合容器内部时,使其尽可能靠近其最右边:(最大宽度-出现的正方形宽度-当前li位置)

Calculated this way, you can animate it with the new offset in positive: 通过这种方式计算,您可以使用正的新偏移量对其进行动画处理:

openDivId.stop().css({'z-index':'9999'}).animate({'opacity':'1', 'left':thisoffset}, 200);

See it working here . 看到它在这里工作。

For elements that "almost" fit, the current system isn't completely precise because of what I already pointed out in my previous comment: the appearing square, even if it were at 0 opacity, would still be inside the containing element ( ($(thumbContainer)) or . thumb-over-team ) and it would add its width to the total width of the container. 对于“几乎”适合的元素,由于我在之前的评论中已经指出,当前系统并不完全精确:出现的正方形,即使它的不透明度为0,仍会在包含元素( ($(thumbContainer))($(thumbContainer)) thumb-over-team ),则会将其宽度添加到容器的总宽度中。

So your conditional may think that there's enough available space in the container to make the expandable element fit, but that would go out of the screen. 因此,您的条件可能会认为容器中有足够的可用空间来使可扩展元素合适,但这将超出屏幕范围。 As an example, notice that there's a horizontal scrollbar from the very beginning, caused by this effect, where your containing .thumb-over-team element doesn't fit in the screen. 例如,请注意,由于这种效果,从一开始就有一个水平滚动条,其中包含的.thumb-over-team元素不适合屏幕。

But I would say that more precision in this point would require a fresh new approach to your system where the appearing .cover elements were out of the containing ul .thumb-over-team 但是我要说的是,在这一点上要提高精度,需要对您的系统采用一种全新的方法,其中出现的.cover元素不在包含的ul .thumb-over-team

Fresh take on the problem, essentially based on the main issue: the expandable text block ( .cover ) used to add its width to the container ( .thumb-over-team ). 基本上基于以下主要问题对问题进行了.cover :可扩展文本块( .cover ),用于将其宽度添加到容器( .thumb-over-team )。 This altered the calculations on available container space, and made the text containers go off screen. 这改变了对可用容器空间的计算,并使文本容器不在屏幕上。

The solution is to make sure the expandable .cover elements aren't contained inside the .thumb-over-team element, so they won't impact the calculations on available width. 解决方案是确保可扩展.cover元素未包含在.thumb-over-team元素内,因此它们不会影响可用宽度上的计算。

Here is a JSFiddle containing this new approach: link . 这是一个包含这种新方法的JSFiddle: link

Explanation of how it works: 解释其工作原理:

The idea was to create a separate element called .cover-container and let's put all the expandable .cover elements in there. 这个想法是创建一个单独的名为.cover-container的元素,然后将所有可扩展的.cover元素放在其中。

We want to associate every image in the li elements in .thumb-over-team with their appropriate .cover (so the first image triggers the first .cover to show, the second image would show the second cover, and so on.) We achieve is by finding out the index of the element that triggered the event: 我们希望将.thumb-over-teamli元素中的每个图像与其相应的.cover (因此第一个图像触发显示的第一个.cover ,第二个图像将显示第二个封面,依此类推。)通过找出触发事件的元素的索引来实现:

thisLiIndex = $this.index() + 1

And then selecting the cover in the matching position: 然后选择匹配位置的封面:

openDivId = $('.cover-container .cover:nth-child(' + thisLiIndex + ')')

The expandable covers shouldn't interfere with the mouseenter or mouseleave events of .thumb-over-team , so we make it to ignore mouse events via CSS: 可膨胀的盖子不应与干扰mouseentermouseleave的事件.thumb-over-team ,所以我们将其忽略通过CSS的鼠标事件:

.cover-container{pointer-events:none;}

Changing from one image to another would automatically trigger new events, so the expanding covers stay visible when the mouse stays on the images, but close automatically when the mouse exits them. 从一幅图像更改为另一幅图像会自动触发新事件,因此,当鼠标停留在图像上时,展开的封面将保持可见状态,但是当鼠标退出图像时,它们将自动关闭。

Since the covers are now outside of $(thumbContainer) , openDivID.outerWidth() does not alter $(thumbContainer).outerWidth() , and we can use that safely in our positioning. 由于封面现在位于$(thumbContainer) ,因此openDivID.outerWidth()不会更改$(thumbContainer).outerWidth() ,我们可以在定位中安全地使用它。

If I understand the placement that you want, for covers that fit, the position is the current offset (position of the li element that triggered the event) plus the width of the image and some subtle margin 如果我了解所需的位置,对于适合的封面,该位置是当前偏移量(触发事件的li元素的位置)加上图像的宽度和一些微妙的边距

imageWidth + rightSeparation + thisoffset

And for covers that won't fit inside of the screen, we keep them just inside of the screen 对于无法容纳在屏幕内部的保护套,我们将其保留在屏幕内部

thisoffset = $(thumbContainer).outerWidth() - openDivId.outerWidth();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM