使Bootstrap卡完全可点击

问题描述 投票:1回答:5

我无法使我的Bootstrap 4卡全部可点击。我正在使用HTML5和Bootstrap 4,并且不能使包含卡可点击。图像和文本是可点击的,但我希望用户能够点击框中的任何位置。我尝试用链接包装它,卡看起来可点击但不完全。感谢帮助

Codepen:https://codepen.io/anon/pen/PVNXgV

HTML

<div class="container">
  <div class="card-deck flex-row flex-nowrap">

    <div class="card">
      <a href="http://google.com"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg" alt="Card image cap"></a>

      <a href="http://google.com">
        <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
        <div class="card-body">
          <a href="http://google.com"><h3 class="card-sub align-middle">Card Title</h3></a>
          <p class="time-card">2 Days Ago</p>
        </div><!-- END CARD-BODY -->
      </a>
    </div><!-- END CARD -->

    </div><!-- END CARD DECK -->
</div><!-- END CONTAINER -->

CSS

.card-container{
  max-width:1400px;
  width:95%;
}

.card-body{
  max-width:250px;
  padding-left:10px;
  margin-top:0px;
  transition:.3s;
  -webkit-transition:.3s;
}

.card-deck{
  margin-bottom:3.2rem;
  display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.front-deck{
  padding-top:12rem;
}

.card{
  margin-right:16px;
  border:none;
  flex: 0 0 auto;
}

.card .card-body:first-of-type{
  border:2px solid #96cecf;
  border-top:0px; 
}
html css html5 twitter-bootstrap bootstrap-4
5个回答
2
投票

你的codepen:https://codepen.io/brooksrelyt/pen/VgjzGr

HTML:

<div class="container">
  <div class="card-deck flex-row flex-nowrap">
<a href="http://google.com">
    <div class="card">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg" alt="Card image cap">


        <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
        <div class="card-body">
<h3 class="card-sub align-middle">Card Title</h3>
          <p class="time-card">2 Days Ago</p>
        </div><!-- END CARD-BODY -->
      </a>
    </div><!-- END CARD -->
  </a>

    </div><!-- END CARD DECK -->
</div><!-- END CONTAINER -->

4
投票

如果你不想使用锚标签(不希望它有一个标签重新设置卡),你可以使用一些自定义的js和样式。像这样的东西。

$(document).ready(() => {
  $(document.body).on('click', '.card[data-clickable=true]', (e) => {
    var href = $(e.currentTarget).data('href');
    window.location = href;
  });
});
.card[data-clickable=true]{
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<div class="container">
  <div class="card-deck flex-row flex-nowrap">

    <div class="card" data-clickable="true" data-href="http://google.com">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg" alt="Card image cap">


      <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
      <div class="card-body">
        <h3 class="card-sub align-middle">Card Title</h3>
        <p class="time-card">2 Days Ago</p>
      </div>
      <!-- END CARD-BODY -->

    </div>
    <!-- END CARD -->

  </div>
  <!-- END CARD DECK -->
</div>
<!-- END CONTAINER -->

.


1
投票

您是否尝试将整张卡片包裹在链接标签中?

.card-container{
	max-width:1400px;
	width:95%;
}

.card-body{
	max-width:250px;
	padding-left:10px;
	margin-top:0px;
	transition:.3s;
	-webkit-transition:.3s;
}

.card-deck{
	margin-bottom:3.2rem;
	display: flex;
  	flex-wrap: nowrap;
  	overflow-x: auto;
  	-webkit-overflow-scrolling: touch;
}

.front-deck{
	padding-top:12rem;
}

.card{
	margin-right:16px;
	border:none;
	flex: 0 0 auto;
}

.card .card-body:first-of-type{
	border:2px solid #96cecf;
	border-top:0px; 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="card-deck flex-row flex-nowrap">
<a href="http://google.com">
    <div class="card">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg" alt="Card image cap">
        <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
        <div class="card-body">
         <h3 class="card-sub align-middle">Card Title</h3>
          <p class="time-card">2 Days Ago</p>
        </div><!-- END CARD-BODY -->
   
    </div><!-- END CARD -->
   </a>
    </div><!-- END CARD DECK -->
</div><!-- END CONTAINER -->

1
投票

您可以将整张卡片包裹在锚标签中,而不是使用多个锚标签。

<div class="container">
      <div class="card-deck flex-row flex-nowrap">
        <a href="http://google.com">

          <div class="card">
            <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg"
              alt="Card image cap">


            <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
            <div class="card-body">
              <h3 class="card-sub align-middle">Card Title</h3>
              <p class="time-card">2 Days Ago</p>
            </div><!-- END CARD-BODY -->
          </div><!-- END CARD -->
        </a>
      </div><!-- END CARD DECK -->

    </div><!-- END CONTAINER -->

1
投票

看起来有很多解决方法,最常见的是将卡包装在锚标签中或使用伪元素。有关更多信息,请参阅https://github.com/twbs/bootstrap/issues/18294

这是一个来自线程的工作码本:https://codepen.io/blazejewicz/pen/OQEeLR/?editors=0100

HTML

  <a class="card" href="#" style="width: 18rem;">
  <img class="card-img-top" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22286%22%20height%3D%22180%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20286%20180%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_161beea174e%20text%20%7B%20fill%3Argba(255%2C255%2C255%2C.75)%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A14pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_161beea174e%22%3E%3Crect%20width%3D%22286%22%20height%3D%22180%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22107.1953125%22%20y%3D%2296.3984375%22%3E286x180%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E" alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title">Card title</h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>

  </div>
</a>
</div>

CSS

a.card {
  color: inherit;
  text-decoration: inherit;
}
a.card button {
  z-index: 1;
}

a.card.disabled,
a.card[disabled] {
  pointer-events: none;
  opacity: .8;
}

JS

(function($) {
  $(() => {
    $('button').on('click', (event) => {
      event.stopImmediatePropagation();
      event.preventDefault();
      alert(`${event.type}: ${event.currentTarget.tagName}`);
    });

    $('a').on('click', (event) => {
      event.preventDefault();
      alert(`${event.type}: ${event.currentTarget.tagName}`);
    });
  });
})(jQuery);
© www.soinside.com 2019 - 2024. All rights reserved.