Prevent CSS tooltip from going out of page/window

后端 未结 5 2037
时光说笑
时光说笑 2021-02-01 04:39

I have a CSS only tooltip which loads a span as a tooltip when you hover the link. However this is positioned with CSS but if the link is near to the t

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 04:49

    although the whole thing needs to recode but you can achieve that with something like this :

    $(".ktooltip").on('mouseover', function(e) {
        var tooltip = $('.ktooltiptext'),
            wt = $(window).scrollTop(), //window top pos
            ww = $(window).outerWidth(), //window width
            tt = tooltip.offset().top, //Tooltip top pos
            tl = tooltip.offset().left, //Tooltip left pos
            tw = tooltip.outerWidth(); //Tooltip Width
    
        tooltip.css({ 'left': '10px', 'right': 'auto', 'top': '-40px' });
    
        if(tt < wt) tooltip.css('top', 0);
        if((tl + tw) > ww) tooltip.css({ 'left': 'auto', 'right': 0 });
    })
    

提交回复
热议问题