【问题标题】:How to Trigger, based on mouse position, variable fonts?如何根据鼠标位置触发可变字体?
【发布时间】:2020-05-30 05:31:42
【问题描述】:

我希望根据我的鼠标移动使用可变字体触发不同的部分。

对于第一部分,一切看起来都很好,但是当我尝试触发第二部分时,它并没有像我预期的那样工作,因为它连接到我猜的第一个部分。

我需要使该部分以正确的方式独立工作(想知道第一个部分在调试模式下的反应如何)

我想知道我必须在我的 Javascript 代码中修改什么,以使我的 sn-p 与我想要的所有部分一起工作,并独立处理它们各自的可变字体交互。有什么想法吗?

  var x = e.pageX - $(this).offset().left;
  var y = e.pageY;
  var $tlSquare = $('.division--top.division--left');
  var $trSquare = $('.division--top.division--right');
  var $blSquare = $('.division--bottom.division--left');
  var $brSquare = $('.division--bottom.division--right');
  var squareWidth = $(this).width(),
    squareHeight = $(this).height();

  $tlSquare.width(x).height(y);
  $trSquare.width(squareWidth - x).height(y);
  $blSquare.width(x).height(squareHeight - y);
  $brSquare.width(squareWidth - x).height(squareHeight - y);

  stretchLetter(false);
});


stretchLetter(false);

$('.square').on('mouseleave', function() {
  $('.division').width('50%').height('50%');
  $('.letter').css('transform', '');
  stretchLetter(false);
});

function stretchLetter(animation) {
  $('.letter').each(function() {
    var parentWidth = $(this).parent().width();
    var parentHeight = $(this).parent().height();
    var thisWidth = $(this).width();
    var thisHeight = $(this).height();
    var widthPercent = parentWidth / thisWidth;
    var heightPercent = parentHeight / thisHeight;
    var timing = animation == true ? .5 : 0;

    TweenMax.to($(this), timing, {
      scaleX: widthPercent,
      scaleY: heightPercent
    })
    //$(this).css('transform', 'scalex('+ widthPercent +') scaley('+ heightPercent +')');
  });
}
body,
html {
  margin: 0;
  padding: 0;
  font-weight: bold;
  font-family: helvetica;
}

section {
  height: 200px;
  background: blue;
  color: white;
  font-size: 28px;
}

.wrapper {
  display: flex;
  justify-content: center;
  /*justify-content: flex-end;*/
  width: 100%;
  height: 100vh;
  //background-color: blue;
  overflow: hidden;
}

.square {
  display: flex;
  flex-wrap: wrap;
  width: 100vh;
  height: 100vh;
  background-color: black;
}

.square-2 {
  display: flex;
  flex-wrap: wrap;
  width: 100vh;
  height: 100vh;
  background-color: yellow;
}

.division {
  //display: flex;
  //align-items: center;
  //justify-content: center;
  width: 50%;
  height: 50%;
  //background-color: red;
  //border: 1px solid white;
  box-sizing: border-box;
}

.letter {
  cursor: -webkit-grab;
  cursor: grab;
}

.letter {
  display: inline-block;
  font-size: 50vh;
  margin: 0;
  padding: 0;
  line-height: .8;
  transform-origin: top left;
  color: white;
}


/* .division:nth-child(1){
    background-color: blue;
}
.division:nth-child(2){
    background-color: red;
}
.division:nth-child(3){
    background-color: green;
}
.division:nth-child(4){
    background-color: orange;
} */

.circle {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 1px solid white;
  background-color: blue;
  box-sizing: border-box;
}
<section>SECTION-01</section>
<main>
  <div class="wrapper">
    <div class="square">
      <div class="division division--top division--left">
        <div class="letter">L</div>
      </div>
      <div class="division division--top division--right">
        <div class="letter">A</div>
      </div>
      <div class="division division--bottom division--left">
        <div class="letter">S</div>
      </div>
      <div class="division division--bottom division--right">
        <div class="letter">T</div>
      </div>
    </div>
</main>
  <section>SECTION-02</section>
  <div class="wrapper">
    <div class="square">
      <div class="division division--top division--left">
        <div class="letter">F</div>
      </div>
      <div class="division division--top division--right">
        <div class="letter">A</div>
      </div>
      <div class="division division--bottom division--left">
        <div class="letter">S</div>
      </div>
      <div class="division division--bottom division--right">
        <div class="letter">T</div>
      </div>
    </div>
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

https://jsfiddle.net/CAT999/ohaf61qp/5/

【问题讨论】:

    标签: javascript java html css


    【解决方案1】:

    更新了 sn-p 以在单个块上工作。我希望这会有所帮助

    var $ = jQuery
    $('.square').on('mousemove', function(e) {
      var x = e.pageX - $(this).offset().left;
      var y = e.pageY;
      var $tlSquare = $(this).find('.division--top.division--left');
      var $trSquare = $(this).find('.division--top.division--right');
      var $blSquare = $(this).find('.division--bottom.division--left');
      var $brSquare = $(this).find('.division--bottom.division--right');
    
      var squareWidth = $(this).width(),
        squareHeight = $(this).height();
    
      $tlSquare.width(x).height(y);
      $trSquare.width(squareWidth - x).height(y);
      $blSquare.width(x).height(squareHeight - y);
      $brSquare.width(squareWidth - x).height(squareHeight - y);
      const childrens = $(this).children().children();
      stretchLetter(false, childrens);
    });
    
    
    /* stretchLetter(false); */
    
    $('.square').on('mouseleave', function() {
      const child = $(this).children();
      child.each(function() {
        $(this).width('50%').height('50%');
      })
      child.children().each(function() {
    
        $(this).css('transform', '');
      })
      const childrens = $(this).children().children();
      stretchLetter(false, childrens);
    });
    
    function stretchLetter(animation, el) {
      el.each(function() {
        var parentWidth = $(this).parent().width();
        var parentHeight = $(this).parent().height();
        var thisWidth = $(this).width();
        var thisHeight = $(this).height();
    
        var widthPercent = parentWidth / thisWidth;
        var heightPercent = parentHeight / thisHeight;
    
        var timing = animation == true ? .5 : 0;
    
        TweenMax.to($(this), timing, {
          scaleX: widthPercent,
          scaleY: heightPercent
        })
        //$(this).css('transform', 'scalex('+ widthPercent +') scaley('+ heightPercent +')');
      });
    }
    $(document).ready(function() {
      const el = $(".square")
        .children()
        .children();
      stretchLetter(false, el);
    });
    body,
    html {
      margin: 0;
      padding: 0;
      font-weight: bold;
      font-family: helvetica;
    }
    
    section {
      height: 200px;
      background: blue;
      color: white;
      font-size: 28px;
    }
    
    .wrapper {
      display: flex;
      justify-content: center;
      /*justify-content: flex-end;*/
      width: 100%;
      height: 100vh;
      //background-color: blue;
      overflow: hidden;
    }
    
    .square {
      display: flex;
      flex-wrap: wrap;
      width: 100vh;
      height: 100vh;
      background-color: black;
    
    }
    
    .square-2 {
      display: flex;
      flex-wrap: wrap;
      width: 100vh;
      height: 100vh;
      background-color: yellow;
    
    }
    
    .division {
      //display: flex;
      //align-items: center;
      //justify-content: center;
      width: 50%;
      height: 50%;
      //background-color: red;
      //border: 1px solid white;
      box-sizing: border-box;
    }
    
    .letter {
      cursor: -webkit-grab;
      cursor: grab;
    }
    
    .letter {
      display: inline-block;
      font-size: 50vh;
      margin: 0;
      padding: 0;
      line-height: .8;
      transform-origin: top left;
      color: white;
    }
    
    
    /* .division:nth-child(1){
    	background-color: blue;
    }
    .division:nth-child(2){
    	background-color: red;
    }
    .division:nth-child(3){
    	background-color: green;
    }
    .division:nth-child(4){
    	background-color: orange;
    } */
    
    .circle {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      border: 1px solid white;
      background-color: blue;
      box-sizing: border-box;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <section>SECTION-01</section>
    <main>
      <div class="wrapper">
        <div class="square">
          <div class="division division--top division--left">
            <div class="letter">L</div>
          </div>
          <div class="division division--top division--right">
            <div class="letter">A</div>
          </div>
    
          <div class="division division--bottom division--left">
            <div class="letter">S</div>
          </div>
          <div class="division division--bottom division--right">
            <div class="letter">T</div>
          </div>
        </div>
    </main>
    <section>SECTION-02</section>
    
    
    
    <!--FOR SINGLE USE THIS ONE <div class="wrapper">
    	<div class="square">
    		<div class="division division--top division--left">
    			<div class="letter">M</div>
    		</div>
    		<div class="division division--top division--right">
    			<div class="letter">A</div>
    		</div>
    		<div class="division division--bottom division--left">
    			<div class="letter">T</div>
    		</div>
    		<div class="division division--bottom division--right">
    			<div class="letter">T</div>
    		</div>
    	</div>
    </div-->
    
    
    
    
    <div class="wrapper">
      <div class="square">
        <div class="division division--top division--left">
          <div class="letter">F</div>
        </div>
        <div class="division division--top division--right">
          <div class="letter">A</div>
        </div>
    
        <div class="division division--bottom division--left">
          <div class="letter">S</div>
        </div>
        <div class="division division--bottom division--right">
          <div class="letter">T</div>
        </div>
      </div>
      </main>
    
    
    
    
    
    
      <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenMax.min.js"></script>

    【讨论】:

    • 非常感谢,但不幸的是,仍然存在一些问题。您知道为什么更新代码后初始位置显示不正确吗?字母间距看起来不同,但是当鼠标输入时,文本再次正确显示..
      第二部分问题仍然是同样的问题,可变字体没有正确显示并且没有作为第一部分做出反应
    • 这只是一种解决方法。你能添加预期和初始图像吗
    • 当然。这里是预期的外观[pasteboard.co/IUE4tv7.png],这里是鼠标输入前的初始值[pasteboard.co/IUE4M8n.png]
    • @CATT 抱歉回复晚了,问题出在您的代码中,您在文档加载时运行代码,但我错过了运行它。问题现已修复,您现在可以检查更新的 sn-p。只需在脚本末尾添加$(document).ready(function() { const el = $(".square") .children() .children(); stretchLetter(false, el); });。
    猜你喜欢
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多