dongshi6529 2014-01-21 18:47
浏览 46
已采纳

为什么我的照片不会出现? [关闭]

I'm using a combination of jQuery and PHP to make a template for a full-width responsive picture with three sizes. What I did worked, but I soon understood that I needed to specify specific ID's to each Div in order to have a custom image for each Div. I suspect something is wrong with my PHP, since it worked correctly before I echoed ID's. Here is an example of how it SHOULD work (check the header picture.)

HTML:
<!-- template 1 -->
<?php 
$imageregular = 'http://placekitten.com/1920/500'; //1920 x 500
$imagesmall   = 'http://placekitten.com/1280/350'; //1280 x 350
$imagemobile  = 'http://placekitten.com/630/250'; // 630 x 250
$id = 'aboutpic00';
include '../fullimage.php';
?>

<!-- template again with different ID --?

<?php 
$imageregular = 'http://placekitten.com/1920/500'; //1920 x 500
$imagesmall   = 'http://placekitten.com/1280/350'; //1280 x 350
$imagemobile  = 'http://placekitten.com/630/250'; // 630 x 250
$id = 'aboutpic01';
include '../fullimage.php';
?>

CSS:

.bg
 {
     height: 350px;
    background-color:#cccccc;
    display:block;
    background-position:center;
    /* and so on for all the media widths.  The CSS probably isn't important, it just establishes the background color and background position. */
 }

PHP template (fullimage.php referenced in HTML)

<script type="text/javascript">
$(document).ready(function(){

//jQuery determines the width of my browser window and executes the following conditions

    var width = $(window).width();
        if(width<=630){
             $("#<?php echo $id;?>").css("background-image", "url(<?php echo $imagemobile;?>)");

//if the width is a certain amount, jQuery will change the CSS on my specified #ID Div using the php echo $id

        }else if(width>=630 && width<1280){
             $("#<?php echo $id;?>").css("background-image", "url(<?php echo $imagesmall;?>)");
        }else if(width>1280){
             $("#<?php echo $id;?>").css("background-image", "url(<?php echo $imageregular;?>)");
        }

//The following function is a repeat of the former, but while the window is resizing. I bet I can combine the two but this fix was easier at the time. 

    $(window).resize(function() {
        var width = $(window).width();
        if(width<=630){
             $("#<?php echo $id;?>").css("background-image", "url(<?php echo $imagemobile;?>)");
        }else if(width>=630 && width<1280){
             $("#<?php echo $id;?>").css("background-image", "url(<?php echo $imagesmall;?>)");
        }else if(width>1280){
             $("#<?php echo $id;?>").css("background-image", "url(<?php echo $imageregular;?>)");
        }
    }); 
});
</script>



<div id="#<?php echo $id;?>" class="bg"></div>  //This is the final HTML output of the PHP include that echoes out the div with the custom ID and "bg" class I specified in the CSS

Right now, I should be getting adorable kittens, but I'm just getting my grey background color. Where am I going wrong?

EDIT: My mistake was that in <div id="#<?php echo $id;?>" class="bg"> I included the "#" which is not supposed to be included in IDs. I'll delete this thread shortly.

  • 写回答

1条回答 默认 最新

  • dongxiusuo9881 2014-01-21 19:03
    关注

    well your problem is because your div id="#aboutpic01" and in jQuery you're looking for "aboutpic01", just delete the "#" symbol from you div

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?