我如何相对定位这个div?

发布于 2024-12-13 18:29:49 字数 4238 浏览 0 评论 0原文

这里是该网站。

我在 div 中包含了“标题”按钮,我试图弄清楚如何将其相对于图像定位在其下方。因此,当我更改图像时,它也会将自己定位在新图像的下方。但是,我什至无法将其相对于图像定位。这是代码:

<?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>

Here's the site.

I contained the "Caption" button in a div and I'm trying to figure out how to position it relative to the image, just under it. So when I change the image, it positions itself just under the new image too. However, I can't even get it to position relative to the image. Here's the code:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能否归途做我良人 2024-12-20 18:29:49

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

I'm not sure where it's being set (probably styles.css), but change the positioning style for the '#center_frame' element from absolute to relative. That appears to fix the problem.

他不在意 2024-12-20 18:29:49

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

将它们都放在图像宽度的容器 div 中,按钮将始终位于其下方:)

为按钮添加一些边距,这样它就不会位于图像的底部。

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

Why the need for static on either the picture or the button?

Put them both in a container div the width of the image and the button will always be below it :)

Add some margin to the button to so it's not sitting right on the bottom of the image.

Like this: http://jsfiddle.net/ChkXw/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文