>  기사  >  웹 프론트엔드  >  JS 및 CSS를 사용하여 그라데이션 배경 효과를 구현하는 코드

JS 및 CSS를 사용하여 그라데이션 배경 효과를 구현하는 코드

不言
不言원래의
2018-06-11 15:27:101880검색

이 글에서는 JS와 CSS로 구현한 아름다운 그라데이션 배경 특수 효과 코드를 주로 소개합니다. 여기에는 페이지 요소 속성의 동적 작동을 위한 JavaScript 관련 기술이 포함되어 있습니다.

이 글에서는 JS+에 대한 예제를 설명합니다. CSS로 구현된 아름다운 그라데이션 배경 효과 코드입니다. 참고용으로 모두와 공유하세요. 세부 사항은 다음과 같습니다.

실행 중인 효과의 스크린샷은 다음과 같습니다.

구체 코드는 다음과 같습니다.

<html>
  <head>
    <title>
      JS配合CSS实现的漂亮渐变背景特效6个实例
    </title>
    <script>
      var setGradient = (function() {
        var p_dCanvas = document.createElement(&#39;canvas&#39;);
        var p_useCanvas = !!(typeof(p_dCanvas.getContext) == &#39;function&#39;);
        var p_dCtx = p_useCanvas ? p_dCanvas.getContext(&#39;2d&#39;) : null;
        var p_isIE =
        /*@cc_on!@*/
        false;
        try {
          p_dCtx.canvas.toDataURL()
        } catch(err) {
          p_useCanvas = false;
        };
        if (p_useCanvas) {
          return function(dEl, sColor1, sColor2, bRepeatY) {
            if (typeof(dEl) == &#39;string&#39;) dEl = document.getElementById(dEl);
            if (!dEl) return false;
            var nW = dEl.offsetWidth;
            var nH = dEl.offsetHeight;
            p_dCanvas.width = nW;
            p_dCanvas.height = nH;
            var dGradient;
            var sRepeat;
            if (bRepeatY) {
              dGradient = p_dCtx.createLinearGradient(0, 0, nW, 0);
              sRepeat = &#39;repeat-y&#39;;
            } else {
              dGradient = p_dCtx.createLinearGradient(0, 0, 0, nH);
              sRepeat = &#39;repeat-x&#39;;
            }
            dGradient.addColorStop(0, sColor1);
            dGradient.addColorStop(1, sColor2);
            p_dCtx.fillStyle = dGradient;
            p_dCtx.fillRect(0, 0, nW, nH);
            var sDataUrl = p_dCtx.canvas.toDataURL(&#39;image/png&#39;);
            with(dEl.style) {
              backgroundRepeat = sRepeat;
              backgroundImage = &#39;url(&#39; + sDataUrl + &#39;)&#39;;
              backgroundColor = sColor2;
            };
          }
        } else if (p_isIE) {
          p_dCanvas = p_useCanvas = p_dCtx = null;
          return function(dEl, sColor1, sColor2, bRepeatY) {
            if (typeof(dEl) == &#39;string&#39;) dEl = document.getElementById(dEl);
            if (!dEl) return false;
            dEl.style.zoom = 1;
            var sF = dEl.currentStyle.filter;
            dEl.style.filter += &#39; &#39; + [&#39;progid:DXImageTransform.Microsoft.gradient( GradientType=&#39;, +( !! bRepeatY), &#39;,enabled=true,startColorstr=&#39;, sColor1, &#39;, endColorstr=&#39;, sColor2, &#39;)&#39;].join(&#39;&#39;);
          };
        } else {
          p_dCanvas = p_useCanvas = p_dCtx = null;
          return function(dEl, sColor1, sColor2) {
            if (typeof(dEl) == &#39;string&#39;) dEl = document.getElementById(dEl);
            if (!dEl) return false;
            with(dEl.style) {
              backgroundColor = sColor2;
            };
          }
        }
      })();
    </script>
    <style>
      body{font:0.75em/1.4 Arial;text-align:left;margin:20px;} hr{clear:both;visibility:hidden;}
      xmp{font:12px/12px "Courier New";background:#fff;color:#666; border:solid
      1px #ccc;} p.example{ border:solid 1px #555;margin:0 20px 20px 0;float:left;
      display:inline;margin:1em;background:#fff;width:300px;padding:40px;color:#222;font:xx-small/1.2
      "Tahoma";text-align:justify;}
    </style>
    <body>
      <p id="example1" class="example">
        CSS特效代码。
      </p>
      <p id="example2" class="example">
        各类编程源码、
      </p>
      <p id="example3" class="example">
        精品软件
      </p>
      <p id="example4" class="example">
        上海世博园
      </p>
      <p id="example5" class="example">
        我家北京天安门
      </p>
      <p id="example6" class="example">
        北京欢迎您!
      </p>
      <script>
        setGradient(&#39;example1&#39;, &#39;#4ddbbe&#39;, &#39;#d449cc&#39;, 1);
        setGradient(&#39;example2&#39;, &#39;#46ddbd&#39;, &#39;#d8b617&#39;, 0);
        setGradient(&#39;example3&#39;, &#39;#c81fc1&#39;, &#39;#bf445f&#39;, 1);
        setGradient(&#39;example4&#39;, &#39;#2285e5&#39;, &#39;#d769eb&#39;, 0);
        setGradient(&#39;example5&#39;, &#39;#8b4286&#39;, &#39;#eac87d&#39;, 1);
        setGradient(&#39;example6&#39;, &#39;black&#39;, &#39;white&#39;, 0);
      </script>
    </body>
</html>

위는 다음과 같습니다. 이 글의 전체 내용이 모든 분들의 학습에 도움이 되기를 바랍니다. 더 많은 관련 내용을 보시려면 PHP 중국어 홈페이지를 주목해주세요!

관련 권장 사항:

CSS를 사용하여 작은 그림이 포함된 LI 아이콘 목록 메뉴 구현

순수한 HTML5+CSS3으로 이미지 회전 생성

HTML+CSS 기반 웹 페이지 슬라이딩 도어 효과 실현

위 내용은 JS 및 CSS를 사용하여 그라데이션 배경 효과를 구현하는 코드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.