【发布时间】:2021-07-14 05:13:39
【问题描述】:
所以我在一个客户的网站上工作,并希望将我在 codepen 中构建的脚本移动到 html elementor 中的 div 容器中,每次我将它添加到网站 (elementor) 中时,它都会将窗口放在底部页面而不是容器中。
更新
试图将 JS 移动到 Div 容器中以添加到网站的特定部分,document.getElementById 会起作用吗?我应该怎么做??
我尝试通过 wordpress 在 web builder 中添加它,但是当我通过 elementor 附加 JS 时,它需要脚本并在页脚下的页面底部应用/查看它,我只希望脚本可以在特定部分中查看。
HTML
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/build/three.min.js"></script>
<!-- OrbitControls.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/controls/OrbitControls.js"></script>
<!-- DRACOLoader.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/loaders/DRACOLoader.js"></script>
<!-- GLTFLoader.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/loaders/GLTFLoader.js"></script>
<div id="container">
*debug* add js element to this container
</div>
JS
let gltf = null;
let mixer = null;
let clock = new THREE.Clock();
let controls;
let camera;
init();
animate();
function init() {
width = window.innerWidth;
height = window.innerHeight;
scene = new THREE.Scene();
let light = new THREE.PointLight( 0xffffcc, 20, 200 );
light.position.set( 4, 30, 80 );
scene.add( light );
let light2 = new THREE.AmbientLight( 0x20202A, 20, 100 );
light2.position.set( 30, -10, 30 );
scene.add( light2 );
camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 10000 );
camera.position.set(0, 3, 10);
let geometry = new THREE.BoxGeometry(100, 5, 100);
let material = new THREE.MeshLambertMaterial({
color: "#707070"
});
let manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
console.log( item, loaded, total );
};
let loader = new THREE.GLTFLoader();
loader.setCrossOrigin( 'anonymous' );
loader.setDRACOLoader( new THREE.DRACOLoader() );
let scale = 0.01;
let url = "https://8ad.studio/wp-content/uploads/3D%20Assets/8AD_LOGO.gltf";
loader.load(url, function (data) {
gltf = data;
let object = gltf.scene;
object.scale.set(scale, scale, scale);
//object.position.y = -5;
//object.position.x = 4;
object.castShadow = true;
object.receiveShadow = true;
let animations = gltf.animations;
if ( animations && animations.length ) {
mixer = new THREE.AnimationMixer( object );
for ( let i = 0; i < animations.length; i ++ ) {
let animation = animations[ i ];
mixer.clipAction( animation ).play();
}
}
scene.add(object);
});
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0xffffff );
renderer.shadowMap.enabled = true;
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.rotateSpeed = 0.3;
controls.zoomSpeed = 0.9;
controls.minDistance = 12;
controls.maxDistance = 12;
controls.minPolarAngle = 0; // radians
controls.maxPolarAngle = Math.PI /2; // radians
controls.enableDamping = true;
controls.dampingFactor = 0.05;
renderer.setSize( width, height );
renderer.gammaOutput = true;
document.body.appendChild( renderer.domElement );
}
function animate() {
requestAnimationFrame( animate );
if (mixer) mixer.update(clock.getDelta());
controls.update();
render();
}
function render() {
renderer.render( scene, camera );
}
【问题讨论】:
-
你能给我们一个这样的代码示例吗?如果没有一个清楚的例子说明发生了什么,就很难找出问题所在。
-
是的,这是它的代码笔。 codepen.io/8AD/pen/wvgYvbj
-
document.body.appendChild(renderer.domElement);是否要更改此行以将 domElement 放入正确的 div 中?
-
请不要使用
script标签。这意味着什么都没有。每个 SO 问题都以一种或另一种方式与脚本有关。标签太糟糕了,过去会被删除,很快就会再次被删除
标签: javascript html elementor