jQuery實(shí)現(xiàn)漂浮效果,漂浮效果js實(shí)現(xiàn),當(dāng)鼠標(biāo)在圖片上方時(shí),圖片停止漂浮,點(diǎn)擊關(guān)閉按鈕可隱藏圖片。
<script src="jquery-1.9.1.min.js"></script>
<div id="imgDiv" style="position:absolute;left:50px;top:50px;z-index:999999; ">
<div id="a" style="width:20px;height:20px;position:absolute;right:0px;background:salmon;text-align: center;">×</div>
<div style="width:300px;height:200px;"><img src="images/no.png" width="300"></div>
</div>
<script>
var xin = true,
yin = true;
var step = 1;
var delay = 10;
var $obj;
$(function() {
$obj = $("#imgDiv");
var time = window.setInterval("move()", delay);
$obj.mouseover(function() {
clearInterval(time)
});
$obj.mouseout(function() {
time = window.setInterval("move()", delay)
});
});
function move() {
var left = $obj.offset().left;
var top = $obj.offset().top;
var L = T = 0; //左邊界和頂部邊界
var R = $(window).width() - $obj.width(); // 右邊界
var B = $(window).height() - $obj.height(); //下邊界
if (left < L) {
xin = true; // 水平向右移動(dòng)
}
if (left > R) {
xin = false;
}
if (top < T) {
yin = true;
}
if (top > B) {
yin = false;
}
left = left + step * (xin == true ? 1 : -1);
top = top + step * (yin == true ? 1 : -1);
// 給div 元素重新定位
$obj.offset({
top: top,
left: left
})
}
$(function() {
$("#a").click(function() {
var b = $("#a").parent();
$(b).remove();
})
})
$("#imgDiv").draggable();
</script>
jquery-1.9.1.min.rar
建站咨詢熱線
029-33273980