【问题标题】:position elements within <div>在 <div> 中定位元素
【发布时间】:2012-09-09 06:26:49
【问题描述】:

我想将所有子图片放在定义的灰色 (#999) div 元素中。我指定了相对的顶部和左侧元素,但图片一直放在我的文档中,而不是仅在我的 div 中相对 - 为什么会这样?我怎样才能让这一切正常进行?

<div style="height:400px;width:600px; background-color:#999999">
<?php

// Hiding notices:
error_reporting(E_ALL^E_NOTICE);

?>

<link rel="stylesheet" type="text/css" href="demo.css" />
<link rel="stylesheet"     href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-darkness/jquery-ui.css" type="text/css" media="all" />
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.2.6.css" />


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="gallery/fancybox/jquery.fancybox-1.2.6.pack.js">        </script>
<script type="text/javascript" src="gallery/script.js"></script>

<div id="main">
<div id="gallery">

<?php

/* Configuration Start */

$thumb_directory = 'gallery/img/thumbs';
$orig_directory = 'gallery/img/original';

$stage_width=400;   // How big is the area the images are scattered on
$stage_height=300;

/* Configuration end */

$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;

/* Opening the thumbnail directory and looping through all the thumbs: */

$dir_handle = @opendir($thumb_directory) or die("There is an error with your image     directory!");

$i=1;
while ($file = readdir($dir_handle)) 
{
/* Skipping the system files: */
if($file=='.' || $file == '..') continue;

$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));

/* Using the file name (withouth the extension) as a image title: */
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);

/* If the file extension is allowed: */ 
if(in_array($ext,$allowed_types))
{
    /* Generating random values for the position and rotation: */
    $left=rand(0,$stage_width);
    $top=rand(0,$stage_height);
    $rot = rand(-45,45);

    if($top>$stage_height-130 && $left > $stage_width-230)
    {
        /* Prevent the images from hiding the drop box */
        $top-=120+130;
        $left-=230;
    }

    /* Outputting each image: */

    echo '
    <div id="pic-'.($i++).'" class="pic" style="height:100px; width:138px; top:'.$top.'px;left:'.$left.'px;background:url('.$thumb_directory.'/'.$file.') no-repeat; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg); position: relative;">
    <a class="fancybox" rel="fncbx" href="/dev/'.$orig_directory.'/'.$file.'" target="_blank">'.$title.'</a>
    </div>';
}
}

/* Closing the directory */
closedir($dir_handle);

?>
</div>
</div>
</div>

链接:http://inetgate.ca/dev/portfolio

【问题讨论】:

    标签: php drupal css html


    【解决方案1】:

    如果要使用topleft 属性,请在div 上设置position: absolute;,然后 position: relative; 在他们的父母 (#gallery) 上。

    你可以使用这两个css规则:

    #gallery {
      position: relative;
    }
    
    .pic {
      position: absolute;
    }
    

    【讨论】:

      【解决方案2】:

      制作包含图片的 div

      position: absolute;
      

      这里

      echo '
      <div id="pic-'.($i++).'" class="pic" style="height:100px; width:138px; top:'.$top.'px;left:'.$left.'px;background:url('.$thumb_directory.'/'.$file.') no-repeat; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg); position: absolute;">
      <a class="fancybox" rel="fncbx" href="/dev/'.$orig_directory.'/'.$file.'" target="_blank">'.$title.'</a>
      </div>';
      

      【讨论】:

      • 我看不懂,你能解释一下吗?
      • 只需将输出每个图像下的代码替换为我在上面添加的行注释即可。一样的,只是把'relative'换成'absolute'的位置样式
      猜你喜欢
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多