如何在首页或底页的元素上动态设置顶级css?

时间:2014-10-09 08:52:19

标签: javascript jquery html css

当元素超过首页或底页时如何动态设置顶部css?

首先,将鼠标悬停在第一张猫图像上。没关系

http://image.ohozaa.com/i/480/7YpWei.jpg

当我将页面滚动到底部并再次拍摄第一张猫图像时,

http://image.ohozaa.com/i/5b5/l2l3dj.jpg
首页上的大猫形象

当我将页面滚动到顶部并再次鼠标3猫图像时,

http://image.ohozaa.com/i/5db/ezZE1b.jpg
底页上的大猫形象

如何将鼠标悬停效果元素的顶部css更改为不超过首页或底页?

http://jsfiddle.net/L7Lyjtp7/

的index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<style type="text/css">
<?PHP
    for($i=0;$i<3;$i++)
    {
?>
        .top_box<?PHP echo $i; ?>{
        left: 99px;
        top: 0px;
        position: absolute;
        border: 1px solid #000;
display: none;
        }
<?PHP
    }
?>
</style>

<?PHP
    for($i=0;$i<3;$i++)
{
?>
<script type="text/javascript">
$(document).ready(function(){    
    $("#thumbnail<?PHP echo $i ; ?>").hover(
        function(){
            var id = $(this).data("id");
            if(id!==undefined){
             $("#" + id).show();   
            }
            $("#top_box<?PHP echo $i; ?>").show(); 

        },function(){
            $("#top_box<?PHP echo $i; ?>").hide(); 

        });
});
</script>
<?PHP
    }
?>
</head>

<br><br><br><br><br><br><br><br><br><br>
<div style=" float: left; position: relative; width: 100%; margin-bottom: 200px; " >
    <div id="top_box0" class="top_box0">
        <img src="http://consciouscat.net/wp-content/uploads/2010/02/cat-licks-lips-200px.jpg">
    </div>
    <span id="thumbnail0" style=" margin: 7px; position: relative;">
        <img border="0" src="http://consciouscat.net/wp-content/uploads/2010/02/cat-licks-lips-200px.jpg" width="80" height="80"/>                
    </span>
</div>
<br>
<div style=" float: left; position: relative; width: 100%; margin-bottom: 200px; " >
    <div id="top_box1" class="top_box1">
        <img src="http://cat-bounce.com/cb.png">
    </div>
    <span id="thumbnail1" style=" margin: 7px; position: relative; ">
        <img border="0" src="http://cat-bounce.com/cb.png" width="80" height="80"/>                  
    </span>
</div>
<br>
<div style=" float: left; position: relative; width: 100%; margin-bottom: 200px; " >
    <div id="top_box2" class="top_box2">
        <img src="http://media.yopi.co.th/prod_pics/12/e/12827.jpg">
    </div>
    <span id="thumbnail2" style=" margin: 7px; position: relative;">
        <img border="0" src="http://media.yopi.co.th/prod_pics/12/e/12827.jpg" width="80" height="80"/>                  
    </span>
</div>

1 个答案:

答案 0 :(得分:0)

您可以将CSS设置为固定,如下所示:JSFIDDLE 但是,如果您希望猫出现在图像旁边,您可以添加jquery来计算图像是否位于“首页”/“底页”并根据需要设置位置。

.top_box0{
    left: 99px;
    top: 0px;
    position: fixed;
    border: 1px solid #000;
    display: none;
}
.top_box1{
    left: 99px;
    top: 0px;
    position: fixed;
    border: 1px solid #000;
    display: none;
}
.top_box2{
    left: 99px;
    top: 0px;
    position: fixed;
    border: 1px solid #000;
    display: none;
}