【问题标题】:Jquery Draggable UI | Change image size when it is dragged into containerjQuery 可拖动 UI |将图像拖入容器时更改图像大小
【发布时间】:2016-03-28 03:46:39
【问题描述】:

我正在使用 Jquery Draggable UI:http://jqueryui.com/draggable/

我正在做一个项目,我可以将四个图像拖到 4 个不同的容器中。我想要做的是将图像的大小更改为放入它的容器的大小。当图像从容器中取出时,它应该恢复到原来的大小。

示例:http://jsfiddle.net/W8yaz/546/

这是我现在的代码:

HTML

<div class="bordered-boxes">
  <ul>
    <li class="squaredotted">
      <div style="width:205px; height: 205px; border: 1px dotted #eee;" class='snappable'>

      </div>
    </li>

    <li class="squaredotted">
      <div class="" style="width:175px; height: 175px; border: 1px dotted #eee;">

      </div>
    </li>

    <li class="squaredotted">
      <div class="" style="width:145px; height: 145px; border: 1px dotted #eee;">

      </div>
    </li>

    <li class="squaredotted">
      <div class="" style="width:110px; height: 110px; border: 1px dotted #eee;">

      </div>
    </li>
  </ul>

  <div class="" style="position: absolute; text-align: left; top: 50%; transform: translateY(-50%); left: 760px; font-weight: bold; font-size: 15px; opacity: .7; font-style: italic;">
    <p>Least
      <br />Important</p>
  </div>

  <div class="" style="position: absolute; text-align: left; top: 50%; transform: translateY(-50%); left: -95px; font-weight: bold; font-size: 15px; opacity: .7; font-style: italic;">
    <p>Most
      <br />Important</p>
  </div>
</div>

<div class="card-wrapper animation">

  <ul>
    <li class="square">
      <div class="container">
        <div class="card" id="incapacitation-icon">
          <div class="front">
            <img src="assets/icons/icon-incapacitation.svg" width="100%" />

            <div class="more-info">
              <img src="assets/icon-info.svg" />
            </div>
          </div>

          <div class="back">
            <section>
              <h4>Incapacitation</h4>

              <p>Confining Dangerous People</p>
            </section>

            <div class="close-icon">
              x
            </div>
          </div>
        </div>
      </div>
    </li>

    <li class="square">
      <div class="container">
        <div class="card" id="deterrence-icon">
          <div class="front">
            <img src="assets/icons/icon-deterrence.svg" width="100%" />

            <div class="more-info">
              <img src="assets/icon-info.svg" />
            </div>
          </div>

          <div class="back">
            <section>
              <h4>Deterrence</h4>

              <p>Setting an Example</p>
            </section>

            <div class="close-icon">
              x
            </div>
          </div>
        </div>
      </div>
    </li>

    <li class="square">
      <div class="container">
        <div class="card" id="rehabilitation-icon">
          <div class="front">
            <img src="assets/icons/icon-rehabilitation.svg" width="100%" />

            <div class="more-info">
              <img src="assets/icon-info.svg" />
            </div>
          </div>

          <div class="back">
            <section>
              <h4>Rehabilitation</h4>

              <p>Breaking the Cycle of Crime</p>
            </section>

            <div class="close-icon">
              x
            </div>
          </div>
        </div>
      </div>
    </li>

    <li style="margin-right:0;" class="square">
      <div class="container">
        <div class="card" id="retribution-icon">
          <div class="front">
            <img src="assets/icons/icon-retribution.svg" width="100%" />

            <div class="more-info">
              <img src="assets/icon-info.svg" />
            </div>
          </div>

          <div class="back">
            <section>
              <h4>Retribution</h4>

              <p>An Eye for An Eye</p>
            </section>

            <div class="close-icon">
              x
            </div>
          </div>
        </div>
      </div>
    </li>

  </ul>

</div>

CSS

.card-wrapper li { 
  height: 203px;
  position: relative;
  list-style-type: none;
}

.card-wrapper .card {
  width: 100%;
  height: 100%;
  position: absolute;
    perspective:800px;
}

.card-wrapper .card div {
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transition-duration: 400ms;
  transition-duration: 400ms;
    z-index:10;
}

.card-wrapper .card .front {
}

.card-wrapper .card .back {
  background: #8d797a;
    z-index:0;
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
  border: 3px solid white;
}

.card-wrapper .card .back h4{
    font-size: 20px;
    color:#4b3839;
    margin-bottom:5px;
    font-weight: bold;
}

.card-wrapper .card .back section {
    padding: 20px;
    text-align: center;
    padding-top:30px;
}

.card-wrapper .card .back p {
    font-size: 18px;
    font-weight: 200;
    color:white;
    margin: 0px;
    line-height: 30px;
}

.card-wrapper .card.flip .front {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
    z-index:0;
}

.card-wrapper .card.flip .back {
    -webkit-transform: rotateY(360deg);
    transform: rotateY(360deg);
    z-index:10;
}

.card-wrapper .more-info {
    font-size: 2em;
    width: 1.5em !important;
    text-align: center;
    line-height: 1.5em;
    background: #FFF;
    color: #fff;
    border-radius: 50px;
    position: absolute !important;
    top: -28px;
    right: -18px;
    height: 1.5em !important;
}

.card-wrapper .close-icon {
    font-size: 2em;
    width: 1.5em !important;
    text-align: center;
    line-height: 1.5em;
    background: #FFF;
    color: #fff;
    border-radius: 50px;
    position: absolute !important;
    top: -28px;
    right: -18px;
    height: 1.5em !important;
}

.card-wrapper .card .close-icon {
    display: none;
}

.card-wrapper .card.flip .more-info {
    display: none;
}

.card-wrapper .card.flip .close-icon {
    display: block;
}

.card-wrapper {
    position: absolute;
    width: 90%;
    left: 0;
    right: 0;
    margin: auto;
    top: 50%;
    transform: translateY(-50%);
}

.bottom-row {
    position: absolute;
    width: 60%;
    left: 0;
    right: 0;
    bottom: 70px;
    margin: auto;
}

.bottom-row li {
    list-style: none;
    width: 20% !important;
    display: inline-block;
    margin-right: 3% !important;
    height: 117px !important;
    position: relative;
}

.bottom-row .back {
    display: none;
}

.bottom-row .more-info {
    display: none;
}

.bottom-row .more-info2 {
    display: none;
}

.bordered-boxes {
    position: absolute;
    width: 75%;
    left: 0;
    right: 0;
    margin: 0 auto;
    display: none;
    top: 50%; 
    transform: translateY(-50%);
}

.bordered-boxes ul {
    display: table;
    width: 100%;
    margin: 0px;
    padding: 0px;
}

.bordered-boxes ul li {
    display: inline-block;
    vertical-align: middle;
    margin-right: 4%;
}

Javascript

$('.square').draggable({
    snap: '.squaredotted',
    snapMode: 'inner',
    snapTolerance: 25,
});

【问题讨论】:

    标签: javascript jquery html css jquery-ui


    【解决方案1】:

    答案在这里: How to find out about the "snapped to" element for jQuery UI draggable elements on snap

    $(".draggable").draggable({
        snap: ".snap",
        stop: function(event, ui) {
            /* Get the possible snap targets: */
            var snapped = $(this).data('draggable').snapElements;
    
            /* Pull out only the snap targets that are "snapping": */
            var snappedTo = $.map(snapped, function(element) {
                return element.snapping ? element.item : null;
            });
    
            /* Process the results in the snappedTo array! */
        }
    });
    

    【讨论】:

    • 感谢您的回复!使用上面的代码,看起来捕捉的图像保留在容器上并且无法移动。对于如何使用上面的代码将图像大小增加到容器的大小,我仍然有点困惑。我更新了小提琴以显示我在说什么:jsfiddle.net/W8yaz/547
    • 我看到有人在这里有更好的答案:stackoverflow.com/questions/5177867/…
    • 我已经阅读了几次该帖子,但仍然对如何根据捕捉到的容器动态调整图像大小感到困惑。我能够像这样将一些东西放在一起:jsfiddle.net/W8yaz/551 但这会使图像在被拖出时保持相同的大小(我需要恢复为 100x100)并且我只能应用一个大小更改。
    • 我看到了你的进步,看来你的方向是正确的。这是捕捉后的一个想法: - 将捕捉的图像复制为 var - 从 DOM 中删除图像 - 将其添加到要捕捉的元素中 - 允许它通过 CSS 100% 高度和宽度自动调整大小
    • 我可以调整图像大小! - jsfiddle.net/W8yaz/556 不幸的是,它不适用于我上面列出的标记。有什么想法吗?
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多