点击小图查看大图的定位功能

用户点击小图一般是要查看小图放大后的大图,或者鼠标移到小图后想要查看详细的信息的大图,这时候要考虑大图的定位问题,以下代码可以参考(需要引入 jq ):


	// 小图  大图  定位
	$(document).on('mouseover', '.smallimg', function(e){
		var src = $(this).attr('src');
		$('body').append('<img src="'+src+'" style="display:none;position:absolute;top:0;left:0;z-index:99999;" class="position_img"/>');
		$('.position_img').fadeIn();
		runPosition($(this), $('.position_img'));
	})
	$(document).on('mouseout', '.smallimg', function(){
		$('.position_img').remove();
	})

	// 实现定位功能 定位到目标元素旁边
	// $target是目标元素  $position是需要定位的元素,放到body层  
	// place是设置位置,如果不设置就按照右下左上的顺序判断能否定位
	function runPosition($target, $position, place){
		var space = 5;
		var offset = $target.offset();
		var top = offset.top;
		var left = offset.left;
		var targetWidth = $target.outerWidth();
		var targetHeight = $target.outerHeight();
		var documentWidth = $(document).width();
		var documentHeight = $(document).height();
		var bottom = documentHeight-top-targetHeight;
		var right = documentWidth-left-targetWidth;

		var positionWidth = $position.outerWidth();
		var positionHeight = $position.outerHeight();
		var deltaTop = top+targetHeight/2 - positionHeight/2;
		var deltaBottom = bottom+targetHeight/2 - positionHeight/2;
		var deltaLeft = left+targetWidth/2 - positionWidth/2;
		var deltaRight = right+targetWidth/2 - positionWidth/2;

		if(!place && (right > positionWidth+space*2) && (documentHeight > positionHeight+space*2)){               // 右边
			$position.css({
				left: left+targetWidth+space+'px',
				top: top+targetHeight/2-positionHeight/2+'px'
			})

			if(deltaTop<space){
				$position.css('top', space+'px')
			} else 
			if(deltaBottom<space){
				$position.css('top', documentHeight-positionHeight-space+'px')
			}
		} else
		if(!place && (bottom > positionHeight+space*2) && (documentWidth > positionWidth+space*2)){             // 下面
			$position.css({
				left: left+targetWidth/2-positionWidth/2+'px',
				top: top+targetHeight+space+'px'
			})

			if(deltaLeft<space){
				$position.css('left', space+'px')
			} else 
			if(deltaRight<space){
				$position.css('left', documentWidth-positionWidth-space+'px')
			}
		} else 
		if(!place && (left > positionWidth+space*2) && (documentHeight > positionHeight+space*2)){                // 左边
			$position.css({
				left: left-positionWidth-space+'px',
				top: top+targetHeight/2-positionHeight/2+'px'
			})

			if(deltaTop<space){
				$position.css('top', space+'px')
			} else 
			if(deltaBottom<space){
				$position.css('top', documentHeight-positionHeight-space+'px')
			}
		} else
		if(!place && (top > positionHeight+space*2) && (documentWidth > positionWidth+space*2)){                 // 上面
			$position.css({
				left: left+targetWidth/2-positionWidth/2+'px',
				top: top-positionHeight-space+'px'
			})

			if(deltaLeft<space){
				$position.css('left', space+'px')
			} else 
			if(deltaRight<space){
				$position.css('left', documentWidth-positionWidth-space+'px')
			}
		} 

		if(place == 'right'){               // 右边
			$position.css({
				left: left+targetWidth+space+'px',
				top: top+targetHeight/2-positionHeight/2+'px'
			})

			if(deltaTop<space){
				$position.css('top', space+'px')
			} else 
			if(deltaBottom<space){
				$position.css('top', documentHeight-positionHeight-space+'px')
			}
		}
		if(place == 'bottom'){             // 下面
			$position.css({
				left: left+targetWidth/2-positionWidth/2+'px',
				top: top+targetHeight+space+'px'
			})

			if(deltaLeft<space){
				$position.css('left', space+'px')
			} else 
			if(deltaRight<space){
				$position.css('left', documentWidth-positionWidth-space+'px')
			}
		}
		if(place == 'left'){                // 左边
			$position.css({
				left: left-positionWidth-space+'px',
				top: top+targetHeight/2-positionHeight/2+'px'
			})

			if(deltaTop<space){
				$position.css('top', space+'px')
			} else 
			if(deltaBottom<space){
				$position.css('top', documentHeight-positionHeight-space+'px')
			}
		}
		if(place == 'top'){                 // 上面
			$position.css({
				left: left+targetWidth/2-positionWidth/2+'px',
				top: top-positionHeight-space+'px'
			})

			if(deltaLeft<space){
				$position.css('left', space+'px')
			} else 
			if(deltaRight<space){
				$position.css('left', documentWidth-positionWidth-space+'px')
			}
		}

	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

codehuicn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值