我如何相对地定位这个div?

时间:2011-11-04 18:50:09

标签: css html positioning

Here是该网站。

我在div中包含了“Caption”按钮,我试图找出如何相对于图像定位它,就在它下面。因此,当我更改图像时,它也会将自己定位在新图像下方。但是,我甚至无法将其相对于图像定位。这是代码:

<?php
    $username = $_GET['username'];
    session_start();
    $_SESSION['username'] = $username;

    //Database Information
    $dbhost = "";
    $dbname = "";
    $dbuser = "";
    $dbpass = "";

    //Connect to database
    mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    //Do the query
    $query = mysql_query("SELECT * FROM images ORDER BY idnum DESC LIMIT 5");

    //Generate an array of all images
    $images = array();
    while($image = mysql_fetch_array($query)) {
    //this adds each image to the images array
    $images[] = $image;
    $image['filename'] = $firstimage;
}
?>

<?php

    // Beginning attempt at a caption script.



?>

<html>
  <head>
   <title>Home - Site in Development</title>
   <link rel="stylesheet" type="text/css" href="styles.css"/>
   <script type="text/javascript">

     // Switches the url to view large image.

    function switchImageUrl(url, width, height) {
     var x = document.getElementById('caption_selection');
        x.style.display = 'none';

     document.getElementById('center_frame').style.backgroundImage = 'url' + '(' + url + ')';
        document.getElementById('center_frame').style.width = width;
        document.getElementById('center_frame').style.height = height;
    }

   </script>

    <script type="text/javascript">

     function selectionToggle() {
       var x = document.getElementById('caption_selection');
       if(x.style.display == 'block')
          x.style.display = 'none';
       else
          x.style.display = 'block';
          x.style.width = '75px';
          x.style.height = '75px';
          x.style.top = '0px';
          x.style.left = '0px';
    }

    </script>

    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
      <script>
      $(document).ready(function() {
        $("#caption_selection").draggable({
        containment : "#center_frame"
        });
        $("#caption_selection").resizable({
        containment : "#center_frame"
        });
      });
      </script>

  </head>
  <body onLoad="CalculateAllImageWidthes()">
   <div id='account_links'>
    <?php
    if ($_SESSION['username']) {
     echo "Welcome $username!";
    } else { ?>
     <a href='login.php'>Login</a> | <a href='register.php'>Register</a>
    <?php } ?>
   </div>

   <h1>Picture Captions</h1>
   <br/>
   <br/>
   <div id="left_bar">
    Submit a picture <a href="upload.php">here</a>.
   <hr/>
   <h2>Top Images</h2>
   <br/>

   <div id="front_pg_images">
    <?php foreach($images as $image) { ?>
     <a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" width="72px" height="58px" id="front_pg_thumbnail"/></a>
     <?php echo $image['name']." - by ".$image['submitter']; ?> <br/>
     <br/>
    <?php } ?>
  </div>

  </div>
   <div id="center_frame" style="width: <?php echo $image['width']; echo "px" ?>; height: <?php echo $image['height']; echo "px" ?>; background-image: url('<?php echo $image['filename'];?>')" >
   <div id="caption_selection" style="display:none">

   </div> 
   </div>
   <div id="toggle_select_container">
    <input type="button" id="toggle_select" onClick="selectionToggle()" value="Caption"/>
   </div>


  </body>
</html>

2 个答案:

答案 0 :(得分:0)

我不确定它的设置位置(可能是styles.css),但是将'#center_frame'元素的定位样式从绝对更改为相对。这似乎解决了这个问题。

答案 1 :(得分:0)

为什么在图片或按钮上需要静电?

将它们放在容器div中,图像的宽度和按钮总是在它下面:)

为按钮添加一些边距,使其不会直接位于图像的底部。

像这样:http://jsfiddle.net/ChkXw/