【问题标题】:Can I use a handlebars template in my app.js file我可以在我的 app.js 文件中使用车把模板吗
【发布时间】:2016-08-06 02:50:50
【问题描述】:

我正在制作一个作品集页面,我希望有一张图片可以淡出并在其背后显示有关图片的文字。我实现了对数据的硬编码。

像这样。

index.html

  <div class="col-sm-6">

    <h4>Freelance Work</h4>

    <img src="/images/andersen.png" class="portfolio_pic" id="test_pic">

    <div id="test_text">
      <p>Site for a structural engineer.</p>
      <p><strong>Languages:</strong> JavaScript, HTML, CSS</p>
      <p><strong>Other:</strong> Git, Bootstrap, GoDaddy Hosting</p>
    </div>

    <p><a href="https://andersen-engineering.com/">Andersen Engineering</a></p>
    <p><a href="https://github.com/nwimmer123/david-excel">GitHub Link</a></p>

  </div>

styles.css

#test_text {
  margin-top: -220px;
  min-height: 210px

}

#test_pic {
  max-height: 250px;
  border: medium groove #660033;
}

app.js

  $('.test_text').hide();

  $('.test_pic').hover(function () {
    $(this).stop().animate({
        opacity: .1
    }, 200);
    $('.test_text').show();
  }, function () {
    $(this).stop().animate({
        opacity: 1
    }, 500);
    $('.test_text').hide();
  });

问题是当我使用此代码从我的猫鼬数据库中引入相同的信息时

index.html

<div class="col-sm-6"> 
  <div class="list-group" id="portfolio_items-list">
    <script id="portfolio_items-template" type="text/x-handlebars-template">
      {{#each portfolio_items}}
        <h4>{{title}}</h4>

        <img src={{image}} class="portfolio_pic" id="test_pic">

        <div id="test_text">
          <p>{{description}}</p>
          <p><strong>Languages: </strong>{{language}}</p>
          <p><strong>Frameworks: </strong>{{framework}}</p>
          <p><strong>Libraries: </strong>{{library}}</p>
          <p><strong>Database: </strong>{{database}}</p>
          <p><strong>Other: </strong> {{other}}</p>
        </div>

        <p><a href={{sitelink}}>{{sitename}}</a></p>

        <p><a href={{githublink}}>GitHub Link</a></p>

        {{/each}}
    </script>
  </div>
</div> 

app.js

  var source = $('#portfolio_items-template').html();
  var template = Handlebars.compile(source);

  //get all database items

  $.get(baseUrl, function (data) {
    var dataHtml = template({ portfolio_items: data});
    $("#portfolio_items-list").append(dataHtml);
  });

那么 test_pic 和 test_text id 没有唯一 ID,因此 javascript hide/show/opacity 技巧不起作用。我在想是否可以将模板引入 app.js 并将每个portfolio_items 数据库ID加载为隐藏/显示/不透明js代码的唯一ID,那么它可能会起作用。另一种选择是通过把手模板使唯一的 id 出现在 index/html 文件中,然后每次使用硬编码的 id 复制 hide/show/opacity js 代码,但这不会是 DRY。

有什么想法吗?

谢谢!

【问题讨论】:

    标签: javascript handlebars.js


    【解决方案1】:

    车把中{{each}} 循环的索引可通过{{@index}} 获得,因此您可以执行id="test-pic-{{@index}} 之类的操作来生成唯一ID。

    FWIW 你也可以在 .css 中完成你正在创建的效果(见下文)。

    .container {
      width:50%;
      height:250px;
      overflow: hidden;
      background: rgba(0, 0, 0, 0.5);
      transition: all .3s ease;
    }
    
    .container:hover {
      background: rgba(0, 0, 0, 0.1);
    }
    
    
    .text {
      font-size: 2em;
      opacity: 0;
      transition: all .3s ease;
    }
    
    .container:hover .text {
     opacity:1;
    
    }
    <div class="container">
      <div class="text">
        <p>hello</p>
        <p>test</p>
      </div>
    </div>

    【讨论】:

    • 我喜欢 css 解决方案,因为它更简单,但它使所有投资组合项目不透明并同时显示文本。我的目标是只让我悬停在不透明的项目上,并在悬停时显示隐藏的 div。
    • 我也喜欢 {{@index}} 的东西。我想如果我把车把放在引号里面,它只会把它放在一个字面上,但显然不是!很酷。
    • 对于 css,您将希望每个投资组合项目在它自己的 .container(或任何类包装 div)中,然后悬停效果将仅适用于该组。
    猜你喜欢
    • 2019-12-13
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2020-01-16
    • 2023-03-10
    相关资源
    最近更新 更多